fnmatch exclude characters from search

Discussion in 'Mac Programming' started by BollywooD, Mar 23, 2010.

  1. BollywooD macrumors 6502

    BollywooD

    Joined:
    Apr 27, 2005
    Location:
    Surfers Paradise
    #1
    I would like to exclude characters from a pattern match, using fnmatch.....


    for example, I need to exclude matching on: all letters, all numbers, and the following – . %


    where do I begin?

    what about RegExKit?

    would that be easier.
     
  2. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #2
    Begin by reading the "Pattern Matching" section of the shell man page.

    [...] Matches any one of the enclosed characters. A pair of charac-
    ters separated by a hyphen denotes a range expression; any char-
    acter that sorts between those two characters, inclusive, using
    the current locale's collating sequence and character set, is
    matched. If the first character following the [ is a ! or a ^
    then any character not enclosed is matched.
    ...​
    Emphasis added.

    Do you already know how to write regex'es? If not, then it's unlikely that writing regex'es will be easier.
     
  3. BollywooD thread starter macrumors 6502

    BollywooD

    Joined:
    Apr 27, 2005
    Location:
    Surfers Paradise
    #3

    Thanks, I didnt even think to check the man page ;)

    would the following rule be valid:

    Code:
    [!a,z/!1,0/!–/!./!%]
    to exclude matching on: all letters, all numbers, and the following – . %
    :confused:
     
  4. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #4
    Is that supposed to be a fnmatch pattern, or is it a regex?
     
  5. BollywooD thread starter macrumors 6502

    BollywooD

    Joined:
    Apr 27, 2005
    Location:
    Surfers Paradise
    #5
    ah, sorry.

    fnmatch pattern
     
  6. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #6
    First question: What happens when you test it?

    Second question: Exactly what are you trying to do with this exclusionary pattern matching?

    Frankly, what you posted doesn't look anything like a shell matching pattern. That's why I asked what you're trying to do.
     
  7. BollywooD thread starter macrumors 6502

    BollywooD

    Joined:
    Apr 27, 2005
    Location:
    Surfers Paradise
    #7

    I want to match a string to an array of strings, however I dont want all letters, all numbers, and the following – . % included within that matching.


    ie....

    string:
    would be a valid match

    stringQ
    string-
    string9

    would not

    this seems to work, i think?
     
  8. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #8
    This doesn't make sense. The fnmatch() function takes two textual args: one is a pattern, the other is a literal string to match against the pattern. In your description, you have a single string and an array of other strings. You don't have any patterns.

    Unless your single string is a pattern, or your array contains patterns, I don't see how fnmatch() can possibly do anything for you.

    Regex won't help either, because it's just fancier pattern matching. It adheres to the same structure as fnmatch: does string X match pattern Y.

    If you want to use pattern-matching in any form, you have to somehow figure out which one of your inputs will be the pattern, and then figure out how to make it be a pattern.

    So now I'm back to the "What are you trying to accomplish?" question.

    Please explain what the single string represents, and what the array of strings represent (or what each string in the array represents), and what you're trying to accomplish by comparing or matching them. We may be able to suggest something. Without knowing what you're trying to accomplish, and based on what you've said so far, pattern-matching alone will never do what you seem to want.


    When I suggested testing it, I meant write a program and test the pattern you proposed against your actual input strings. If you have to say "this seems to work" or "I think?", then I can only assume you haven't performed an actual test of an actual program. Otherwise you'd have a definite answer: it works or it doesn't. And then if you'd posted "It doesn't work", I would have said "Post your code".
     
  9. BollywooD thread starter macrumors 6502

    BollywooD

    Joined:
    Apr 27, 2005
    Location:
    Surfers Paradise
    #9
    As far as I can tell, the code posted earlier does seem to work....
    at least good enough, for what I want.
    :eek:
     

Share This Page