bodacious
bodacious

Reputation: 6705

How can I simplify this regular expression?

The format I'm trying to match is:

# (Apple push notification codes)
"11a735e9 9f696c2f 700b2700 728042c6 137eeb7a 8442c27d 40e59d9e 3c7e0de7"

The simplest expression I can think of is: /((\w{8}\s){7}\w{8})/i

Can anyone think of a simpler one?

(I'm using Ruby regular expressions)

UPDATE - thanks to user1096188, I've removed \d - this is included in \w

Upvotes: 2

Views: 134

Answers (4)

nkm
nkm

Reputation: 5914

> "11a735e9 9f696c2f 700b2700 728042c6 137eeb7a 8442c27d 40e59d9e 3c7e0de7".match(/((\w{8}\s)+)/)
> $&
 => "11a735e9 9f696c2f 700b2700 728042c6 137eeb7a 8442c27d 40e59d9e 3c7e0de7"

Upvotes: 0

SimonMayer
SimonMayer

Reputation: 4925

Taking @zapthedingbat's solution one stage further, it looks like the code only contains hexadecimal characters (0-9 and a-f) and spaces. So you could possibly sacrifice a little simplicity for accuracy.

I'm making an assumption, but I suspect letters g to z are invalid. If the format is hexadecimal only (you should check Apple's documentation to be sure), a tighter match would be:

/(?:[0-9a-f]{8}\b\s?){8}/

EDIT

In fact, in Ruby, it looks like you should be able to do:

/(?:\h{8}\b\s?){8}/

Upvotes: 0

user123444555621
user123444555621

Reputation: 153064

You can detect a word boundary using \b, and use (?: to prevent capturing groups

/(?:\w{8}\b\s?){8}/

Upvotes: 3

Sam Greenhalgh
Sam Greenhalgh

Reputation: 6136

You could do this if the end of the match is the end of the whole string. (\w{8}(:?\s|$)){7}

Upvotes: 2

Related Questions