Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

rbert

macrumors newbie
Original poster
Nov 15, 2009
12
0
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);
 
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.
 
ahhh... i got match now:

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

Thank you for your tip!
 
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
 
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.
 
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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.