Regex does not work with NSPredicate

Discussion in 'Mac Programming' started by rbert, Feb 14, 2010.

  1. rbert macrumors newbie

    Joined:
    Nov 15, 2009
    #1
    Hello,

    I am trying to get match to 0x00306008, the regex is working well when i test it on http://www.regex-tester.de/regex.html but when i put it into this code it does not work any more, can you please take a look what is wrong, thank you!

    PHP:
    NSString *stringx = @"aaaaaaaaaaaaaa0x00306008bbbbbbbbbbbbbbbbb";
    NSString *regex = @"0x[0-9a-fA-F]{8}";
    NSPredicate *regexPredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@"regex];
    BOOL match = [regexPredicate evaluateWithObject:stringx];
    NSLog(@"Match: %i"match);
     
  2. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #2
    Your regex doesn't match your string.

    The reason is that your regex doesn't include a leading and trailing ".*" to match the a's and b's.

    There are some other problems with your predicate string, so you should probably read the "Regular Expressions" subsection of the following in more detail, and try coding and running the examples shown:

    http://developer.apple.com/mac/libr...oa/conceptual/Predicates/Articles/pUsing.html

    Or if you're only looking for an exact match to "0x00306008 ", you might want to simplify things by searching the NSString for the specific substring rather than a regex. Read the NSString reference doc.
     
  3. rbert thread starter macrumors newbie

    Joined:
    Nov 15, 2009
    #3
    ahhh... i got match now:

    NSString *regex = @".*(0x[0-9a-fA-F]{8}).*";

    Thank you for your tip!
     
  4. jared_kipe macrumors 68030

    jared_kipe

    Joined:
    Dec 8, 2003
    Location:
    Seattle
    #4
    God regx blows my mind. Anyone have a favorite book or how to?
     
  5. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #5
    I have, and like:
    http://oreilly.com/catalog/9780596004156

    but the hardest part to me is expressing a regex in a tool or language's system. I.e. I know I want n whitespace characters, 1-5 alphanumerics, then n whitespace characters. How one expresses that in the bevy of tools and languages can be quite different.

    Obviously sometimes you have to parse existing data, and "make it work", but I really try to avoid regexes in permanent code. It's fine while I'm trying to parse a log, but in code I feel like many lines of comments are needed to explain any non-trivial regex.

    -Lee
     
  6. jared_kipe macrumors 68030

    jared_kipe

    Joined:
    Dec 8, 2003
    Location:
    Seattle
    #6
    One of the first things I did in objective-c was write a calculator that could take a wide variety of "regular" math input and spit out an answer. So like

    [3*5/(4sin(1.45)+2)-6.5(3+4+-(2/1))]/{2/6^3+sqrt(2)}
    -26.949 (Yes I used the program to test it)
    And I don't try to say that code is pretty. But it is super ***** useful!

    There are lots of weird things like arrays of positions for [](){} characters that get made into NSRange like objects to test if they overlap or which one is the root level operator and so on. I've always wondered how it could be using regx.
     
  7. Detrius macrumors 68000

    Joined:
    Sep 10, 2008
    Location:
    Asheville, NC
    #7
    If you want to see regular expressions on steroids, check out ANTLR for a right-to-left parser, and the combination of flex & bison for left-to-right parsers.

    ANTLR is much cleaner, but each has its limitations.
     

Share This Page