Regex Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
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, 03: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, 06:02 PM
requinix's Avatar
requinix requinix is offline
nuts - top 5 poster again :(
Click here for more information.
 
Join Date: Mar 2007
Location: Washington, USA
Posts: 5,301 requinix User rank is General 30th Grade (Above 100000 Reputation Level)requinix User rank is General 30th Grade (Above 100000 Reputation Level)requinix User rank is General 30th Grade (Above 100000 Reputation Level)requinix User rank is General 30th Grade (Above 100000 Reputation Level)requinix User rank is General 30th Grade (Above 100000 Reputation Level)requinix User rank is General 30th Grade (Above 100000 Reputation Level)requinix User rank is General 30th Grade (Above 100000 Reputation Level)requinix User rank is General 30th Grade (Above 100000 Reputation Level)requinix User rank is General 30th Grade (Above 100000 Reputation Level)requinix User rank is General 30th Grade (Above 100000 Reputation Level)requinix User rank is General 30th Grade (Above 100000 Reputation Level)requinix User rank is General 30th Grade (Above 100000 Reputation Level)requinix User rank is General 30th Grade (Above 100000 Reputation Level)requinix User rank is General 30th Grade (Above 100000 Reputation Level)requinix User rank is General 30th Grade (Above 100000 Reputation Level)requinix User rank is General 30th Grade (Above 100000 Reputation Level)  Folding Points: 161153 Folding Title: Super Ultimate Folder - Level 1Folding Points: 161153 Folding Title: Super Ultimate Folder - Level 1Folding Points: 161153 Folding Title: Super Ultimate Folder - Level 1Folding Points: 161153 Folding Title: Super Ultimate Folder - Level 1Folding Points: 161153 Folding Title: Super Ultimate Folder - Level 1Folding Points: 161153 Folding Title: Super Ultimate Folder - Level 1
Time spent in forums: 4 Months 9 h 54 m 20 sec
Reputation Power: 2967
Send a message via MSN to requinix
Using a regular expression to do this is silly.
Code:
^((?!BRANCH).)*$
__________________
How to ask a PHP question | My stuff

Reply With Quote
  #3  
Old January 28th, 2009, 09:38 AM
prometheuzz prometheuzz is offline
User 165270
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2005
Posts: 496 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 9 h 15 m 23 sec
Reputation Power: 933
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, 03: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, 04:47 AM
prometheuzz prometheuzz is offline
User 165270
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2005
Posts: 496 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 9 h 15 m 23 sec
Reputation Power: 933
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, 11: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, 02:02 AM
prometheuzz prometheuzz is offline
User 165270
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2005
Posts: 496 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 9 h 15 m 23 sec
Reputation Power: 933
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, 01:08 PM
latros latros is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2004
Posts: 187 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 36 m 6 sec
Reputation Power: 61
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


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




 Free IT White Papers!
 
How to Present Effectively Online
This white paper offers practical and actionable advice on the key steps that any presenter should consider as they plan and execute a Webinar or online meeting.

 
Open Source Security Myths
Open Source Software (OSS) is computer software whose source code is available to the general public with relaxed or non-existent intellectual property restrictions (or arrangement such as the public domain), and is usually developed with the input of many contributors.

 
Power and Cooling Capacity Management for Data Centers
This paper describes the principles for achieving power and cooling capacity management.

 
Scalable, Fault-Tolerant NAS for Oracle - The Next Generation
For several years NAS has been evolving as a storage alternative for Oracle databases, and for good reason: NAS is quite often the simplest, most cost-effective storage approach for Oracle. Learn about the benefits that HP's approach to scalable NAS brings to Oracle environments in this comprehensive white paper.

 
Understanding Web Application Security Challenges
This white paper discusses many common threats and preventive measures for Web application security, and explains what you can do to help protect your organization.

 

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




© 2003-2009 by Developer Shed. All rights reserved. DS Cluster 6 Hosted by Hostway
Stay green...Green IT