 |
BOOK: Beginning Regular Expressions  | This is the forum to discuss the Wrox book Beginning Regular Expressions by Andrew Watt; ISBN: 9780764574894 |
|
Welcome to the p2p.wrox.com Forums.
You are currently viewing the BOOK: Beginning Regular Expressions section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|
|

December 1st, 2005, 03:02 PM
|
|
Registered User
|
|
Join Date: Dec 2005
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Basic Regular Expressions help needed
I am VERY new the regular expressions as it isn't really supported by my programming platform. However, a third-party plug-in I am using does support regular expressions. I am trying to create a regular expression to match on a specific pattern followed by any character except a space and then more characters. I have used "=Case.*" to match on "Case" with any characters after "Case". That works great. I can't seem to find the correct syntax for matching "Case" with any characters after "Case" except a space character.
For instance, I want to match "Case:" or "Case-", but not "Case ".
Any help would be greatly appreciated.
|
|

December 2nd, 2005, 08:22 AM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 1,212
Thanks: 0
Thanked 1 Time in 1 Post
|
|
well the Not character is usually ^ so
Case[^ ]
should work for you
|
|

December 2nd, 2005, 10:48 AM
|
|
Registered User
|
|
Join Date: Dec 2005
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Thanks pgtips. So, if I wanted to match "Case: xxyy" and not "Case xxyy", where xxyy is any characters, would the syntax be, "Case[^ ].*"?
|
|

December 2nd, 2005, 10:50 AM
|
|
Registered User
|
|
Join Date: Dec 2005
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Thanks pgtips!
So, if I wanted to match "Case: xxyy" or "Case- xxyy" but not "Case xxyy", where xxyy is any characters, would the syntax be "Case[^ ].*"?
|
|

December 2nd, 2005, 10:53 AM
|
|
Registered User
|
|
Join Date: Dec 2005
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Sorry about the double post, I got an error when I tried to post the first one so I posted again.
Anyway, I answered my own question. pgtips you saved the day. THANK YOU!!!!
|
|

May 27th, 2006, 06:54 AM
|
|
Registered User
|
|
Join Date: May 2006
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
How to select cities containing non-matching pattern using ^ operand.
For example:
London
NewYork
Delhi
Mountainview
how to get cities except "Delhi"
Please suggest. Thanks.
|
|

May 30th, 2006, 05:23 AM
|
|
Authorized User
|
|
Join Date: Apr 2006
Posts: 24
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Hi neuron,
In theory this is possible with regex, but programmatorically this doesn't make sense. If you are processing the string in software, just parse it to an array for example, and then when processing them all, do something special if it is 'Dehli'. Anyway, just for the theory, it can be done like this:
(Assuming that all your cities are in a file, separated with a newline and that you do a multiline match)
^(?!Delhi$)\w+$
I tried for a minute to do it with negated character classes as you request it, but it is quite hard, and since it doesn't make any sense I'm not gonna waste more time on it...
greetz
|
|

May 31st, 2006, 04:33 AM
|
|
Registered User
|
|
Join Date: May 2006
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Thanks, you understood my requirement but is it practically possible to to simply negate a particular string/group of characters.
I can add ^ in [] like [^Delhi] but it means characters except D,e,l,h,i but not the word except "Delhi"
|
|

May 31st, 2006, 04:47 AM
|
|
Authorized User
|
|
Join Date: Apr 2006
Posts: 24
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Dear neuron,
(?!Delhi$) is a lookahead which will match a position (as opposed to a character, it will not consume anything, or change the matching position after it is checked. eg it matches a place between two characters) which is not followed by the word 'Dehli' and the end of the line. So basically this negates this particular group of characters in this order. The end of the line is important to assure that you don't match a cityname that starts with 'Dehli', quite unlikely in this case, but just to be correct.
For more on lookaheads and behinds see: http://be2.php.net/manual/nl/referen...ern.syntax.php and scroll down to assertions. The whole article is quite bulky, but is well worth your time. Once you understand it, i can garantee you that regex are great fun to play with.... have fun
|
|

May 31st, 2006, 07:03 AM
|
|
Registered User
|
|
Join Date: May 2006
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Thanks ufo, really nice document :)
|
|
 |