Regex Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming Languages - MoreRegex Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old January 27th, 2009, 02:58 AM
arshsidhu arshsidhu is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2009
Posts: 3 arshsidhu User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 36 m 21 sec
Reputation Power: 0
RegEx to match a string only if it does'nt have a particular string within it

Hi All

Requesting help on this one. I am working on a JAVA based tool 'webMethods' which provides built-in functionality to replace char/string from a given input, it also excepts RegEx to match the search string.

My requirement to create a RegEx which can be supplied to the above built-in functionality to match any string (that can contain new lines and other white space characters) only if it doesnt have a particular word say BRANCH.

Examples of positive matches can be-

* The bank of America.
* The #123 bank.
* @$%# (*%&.

Examples of negative matches can be-

* BRANCH
* The #123 BRANCH of this bank.
* This is the last BRANCH.
* BRANCH BRANCH


I tried using the expression [\s\S\s]*(?!BRANCH)[\s\S\s]*, but this does'nt work for all the scenarios.


Thanks !!

Reply With Quote
  #2  
Old January 27th, 2009, 05:02 PM
requinix's Avatar
requinix requinix is offline
Still alive
Dev Shed God 16th Plane (12500 - 12999 posts)
 
Join Date: Mar 2007
Location: Washington, USA
Posts: 12,877 requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)  Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1
Time spent in forums: 5 Months 1 Week 5 Days 7 h 57 m 15 sec
Reputation Power: 8977
Send a message via AIM to requinix Send a message via MSN to requinix Send a message via Yahoo to requinix Send a message via Google Talk to requinix
Using a regular expression to do this is silly.
Code:
^((?!BRANCH).)*$

Reply With Quote
  #3  
Old January 28th, 2009, 08:38 AM
prometheuzz prometheuzz is offline
User 165270
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2005
Posts: 497 prometheuzz User rank is General (90000 - 100000 Reputation Level)prometheuzz User rank is General (90000 - 100000 Reputation Level)prometheuzz User rank is General (90000 - 100000 Reputation Level)prometheuzz User rank is General (90000 - 100000 Reputation Level)prometheuzz User rank is General (90000 - 100000 Reputation Level)prometheuzz User rank is General (90000 - 100000 Reputation Level)prometheuzz User rank is General (90000 - 100000 Reputation Level)prometheuzz User rank is General (90000 - 100000 Reputation Level)prometheuzz User rank is General (90000 - 100000 Reputation Level)prometheuzz User rank is General (90000 - 100000 Reputation Level)prometheuzz User rank is General (90000 - 100000 Reputation Level)prometheuzz User rank is General (90000 - 100000 Reputation Level)prometheuzz User rank is General (90000 - 100000 Reputation Level)prometheuzz User rank is General (90000 - 100000 Reputation Level)prometheuzz User rank is General (90000 - 100000 Reputation Level)prometheuzz User rank is General (90000 - 100000 Reputation Level) 
Time spent in forums: 5 Days 10 h 14 m 35 sec
Reputation Power: 936
As already mentioned by requinix, regex isn't well suited to negate something (except a single character). Regex is more intended to match strings, not "not match" them.

Anyway, if you find requinix' answer a bit confusing, you may find this approach a bit easier to comprehend:

Code:
^(?!.*?BRANCH).*$

Reply With Quote
  #4  
Old February 6th, 2009, 02:44 AM
arshsidhu arshsidhu is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2009
Posts: 3 arshsidhu User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 36 m 21 sec
Reputation Power: 0
Thanks a lot for the RegEx pattern and please accept my apologies for late response.

This pattern is working for all the possible cases except for those where there is newline in the string.For example-

Positive match-

* The
#123 of this bank.

Negative match -

* The
#123 BRANCH of this
bank. .

Is there a way we can add the newline option in the RegEx pattern.

Thanks !!


arsh

Reply With Quote
  #5  
Old February 6th, 2009, 03:47 AM
prometheuzz prometheuzz is offline
User 165270
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2005
Posts: 497 prometheuzz User rank is General (90000 - 100000 Reputation Level)prometheuzz User rank is General (90000 - 100000 Reputation Level)prometheuzz User rank is General (90000 - 100000 Reputation Level)prometheuzz User rank is General (90000 - 100000 Reputation Level)prometheuzz User rank is General (90000 - 100000 Reputation Level)prometheuzz User rank is General (90000 - 100000 Reputation Level)prometheuzz User rank is General (90000 - 100000 Reputation Level)prometheuzz User rank is General (90000 - 100000 Reputation Level)prometheuzz User rank is General (90000 - 100000 Reputation Level)prometheuzz User rank is General (90000 - 100000 Reputation Level)prometheuzz User rank is General (90000 - 100000 Reputation Level)prometheuzz User rank is General (90000 - 100000 Reputation Level)prometheuzz User rank is General (90000 - 100000 Reputation Level)prometheuzz User rank is General (90000 - 100000 Reputation Level)prometheuzz User rank is General (90000 - 100000 Reputation Level)prometheuzz User rank is General (90000 - 100000 Reputation Level) 
Time spent in forums: 5 Days 10 h 14 m 35 sec
Reputation Power: 936
Quote:
Originally Posted by arshsidhu
Thanks a lot for the RegEx pattern and please accept my apologies for late response.


No problem.

Quote:
Originally Posted by arshsidhu
This pattern is working for all the possible cases except for those where there is newline in the string.For example-

Positive match-

* The
#123 of this bank.

Negative match -

* The
#123 BRANCH of this
bank. .

Is there a way we can add the newline option in the RegEx pattern.

Thanks !!


arsh


That is because the DOT meta character matches any character except new line characters. So, when your input consists of multiple lines and the first line does not have your predefined "forbidden" string, it will fail (as you have noticed).
To overcome this, you would have to "tell" the regex engine to let the DOT meta character match any character possible (so, including new line characters!). You can do that by adding the DOT-ALL flag ("(?s)") to your regex. So, here's requinix' proposal (I like it better than what I proposed) including the DOT-ALL flag:

Code:
^(?s)((?!BRANCH).)*$


Good luck.

Reply With Quote
  #6  
Old February 8th, 2009, 10:51 PM
arshsidhu arshsidhu is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2009
Posts: 3 arshsidhu User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 36 m 21 sec
Reputation Power: 0
Smile

Quote:
Originally Posted by prometheuzz
No problem.

That is because the DOT meta character matches any character except new line characters. So, when your input consists of multiple lines and the first line does not have your predefined "forbidden" string, it will fail (as you have noticed).
To overcome this, you would have to "tell" the regex engine to let the DOT meta character match any character possible (so, including new line characters!). You can do that by adding the DOT-ALL flag ("(?s)") to your regex. So, here's requinix' proposal (I like it better than what I proposed) including the DOT-ALL flag:

Code:
^(?s)((?!BRANCH).)*$


Good luck.




Thanks a lot , the last pattern worked for me.
I was not aware of the DOT-ALL flag and was trying to add '\n' to the pattern, something like this .....
Code:
^(?:(?!BRANCH)[\s.\s]*\n?)*$
but it wasn't looking good either

Thanks again !!

Reply With Quote
  #7  
Old February 9th, 2009, 01:02 AM
prometheuzz prometheuzz is offline
User 165270
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2005
Posts: 497 prometheuzz User rank is General (90000 - 100000 Reputation Level)prometheuzz User rank is General (90000 - 100000 Reputation Level)prometheuzz User rank is General (90000 - 100000 Reputation Level)prometheuzz User rank is General (90000 - 100000 Reputation Level)prometheuzz User rank is General (90000 - 100000 Reputation Level)prometheuzz User rank is General (90000 - 100000 Reputation Level)prometheuzz User rank is General (90000 - 100000 Reputation Level)prometheuzz User rank is General (90000 - 100000 Reputation Level)prometheuzz User rank is General (90000 - 100000 Reputation Level)prometheuzz User rank is General (90000 - 100000 Reputation Level)prometheuzz User rank is General (90000 - 100000 Reputation Level)prometheuzz User rank is General (90000 - 100000 Reputation Level)prometheuzz User rank is General (90000 - 100000 Reputation Level)prometheuzz User rank is General (90000 - 100000 Reputation Level)prometheuzz User rank is General (90000 - 100000 Reputation Level)prometheuzz User rank is General (90000 - 100000 Reputation Level) 
Time spent in forums: 5 Days 10 h 14 m 35 sec
Reputation Power: 936
Quote:
Originally Posted by arshsidhu
Thanks a lot , the last pattern worked for me.
I was not aware of the DOT-ALL flag and was trying to add '\n' to the pattern, something like this .....
Code:
^(?:(?!BRANCH)[\s.\s]*\n?)*$
but it wasn't looking good either

Thanks again !!


You're welcome.

Reply With Quote
  #8  
Old April 2nd, 2009, 12:08 PM
latros latros is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2004
Posts: 233 latros User rank is Second Lieutenant (5000 - 10000 Reputation Level)latros User rank is Second Lieutenant (5000 - 10000 Reputation Level)latros User rank is Second Lieutenant (5000 - 10000 Reputation Level)latros User rank is Second Lieutenant (5000 - 10000 Reputation Level)latros User rank is Second Lieutenant (5000 - 10000 Reputation Level)latros User rank is Second Lieutenant (5000 - 10000 Reputation Level)latros User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 2 Days 16 h 38 m 21 sec
Reputation Power: 75
You dont need a regex to do this. Use InString

Code:
mystr = "bank of america"

if instr(lcase(mystr), "bank")
{
    // string contains the word "bank"
}
else
{
    // string does not contain the word "bank"
}


(syntax varies based on what language you're using. If JavaScript then its not built in, so you will need a prototype, which can be easily found by googling "javascript instring prototype")

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming Languages - MoreRegex Programming > RegEx to match a string only if it does'nt have a particular string within it

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap