Skip to main content
GameDev.net gamedev.net
🔒 Locked

Regular Expressions Help

Started by NecoSpes Aug 27, 2008 at 8:22 AM 2 replies 700+ views
Original Post
NecoSpes
NecoSpes
Is there a way I can do "[^(WORD)]+" ? So that, it will match to every character in this sequence until "word": "abcaksjnfakcikjWORDkjancjkl". I want to parse certain words out of a very long pattern, and it would be a lot easier if I could do something like what I said above.
Bregma
Bregma
Quote:
Original post by NecoSpes
Is there a way I can do "[^(WORD)]+" ? So that, it will match to every character in this sequence until "word": "abcaksjnfakcikjWORDkjancjkl".

  ".*WORD"
?

Can you be more specific about what you want to do? Capture everything up to WORD? Find the position of WORD within the match text?
Stephen M. Webb
Professional Free Software Developer
NecoSpes
NecoSpes
Something like this:

"Item: Laptop \<\a\>\20 in Stock Price: 1,000"

I want to get the Laptop, 20, and 1,000 out.

And to clarify, ".*WORD" will match "abcaksjnfakcikjWORD" ? The .* is like a wild card?
Bregma
Bregma
Quote:
Original post by NecoSpes
Something like this:

"Item: Laptop \<\a\>\20 in Stock Price: 1,000"

I want to get the Laptop, 20, and 1,000 out.

Ah, so you want to capture certain subexpressions.

Perhaps you want something like this (ECMAScript/Perl5 regexes, syntax varies between regex dialects).
  "Item:\s+(.*)\s*<.*>(\d+)\s+in Stock.*Price:\s+([\d,.]*)"

The item will be in capture 1, the quantity in capture 2, and the price in capture 3. The captures are defined by the subexpressions within parentheses.

Quote:

And to clarify, ".*WORD" will match "abcaksjnfakcikjWORD" ? The .* is like a wild card?

Yes, that expression will match. The dot is the wildcard, the star is the Kleene closure (repeat zero or more times until the logest possible match is made).
Stephen M. Webb
Professional Free Software Developer

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.