Wrox Programmer Forums
|
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
 
Old December 1st, 2005, 03:02 PM
Registered User
 
Join Date: Dec 2005
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default 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.

 
Old December 2nd, 2005, 08:22 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,212
Thanks: 0
Thanked 1 Time in 1 Post
Default

well the Not character is usually ^ so
Case[^ ]
should work for you
 
Old December 2nd, 2005, 10:48 AM
Registered User
 
Join Date: Dec 2005
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks pgtips. So, if I wanted to match "Case: xxyy" and not "Case xxyy", where xxyy is any characters, would the syntax be, "Case[^ ].*"?

 
Old December 2nd, 2005, 10:50 AM
Registered User
 
Join Date: Dec 2005
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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[^ ].*"?

 
Old December 2nd, 2005, 10:53 AM
Registered User
 
Join Date: Dec 2005
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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!!!!

 
Old May 27th, 2006, 06:54 AM
Registered User
 
Join Date: May 2006
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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.

 
Old May 30th, 2006, 05:23 AM
ufo ufo is offline
Authorized User
 
Join Date: Apr 2006
Posts: 24
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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
 
Old May 31st, 2006, 04:33 AM
Registered User
 
Join Date: May 2006
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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"

 
Old May 31st, 2006, 04:47 AM
ufo ufo is offline
Authorized User
 
Join Date: Apr 2006
Posts: 24
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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
 
Old May 31st, 2006, 07:03 AM
Registered User
 
Join Date: May 2006
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks ufo, really nice document :)






Similar Threads
Thread Thread Starter Forum Replies Last Post
Regular Expressions mega Beginning PHP 1 February 5th, 2007 05:31 PM
Regular Expressions Help Needed mak1600 BOOK: Beginning Regular Expressions 4 January 29th, 2007 04:35 PM
Regular expressions on C# hideway C# 2 November 27th, 2006 05:08 PM
regular expressions help kyootepuffy Classic ASP Databases 2 September 10th, 2003 01:37 PM
Regular Expressions Dave Doknjas C# 1 August 9th, 2003 12:05 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.