Regex does not work with NSPredicate

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

  1. rbert macrumors newbie

    Nov 15, 2009

    I am trying to get match to 0x00306008, the regex is working well when i test it on 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!

    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

    Aug 9, 2009
    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:

    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

    Nov 15, 2009
    ahhh... i got match now:

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

    Thank you for your tip!
  4. jared_kipe macrumors 68030


    Dec 8, 2003
    God regx blows my mind. Anyone have a favorite book or how to?
  5. lee1210 macrumors 68040


    Jan 10, 2005
    Dallas, TX
    I have, and like:

    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.

  6. jared_kipe macrumors 68030


    Dec 8, 2003
    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

    -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

    Sep 10, 2008
    Asheville, NC
    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