The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages - More
> Regex Programming
|
RegEx to match a string only if it does'nt have a particular string within it
Discuss RegEx to match a string only if it does'nt have a particular string within it in the Regex Programming forum on Dev Shed. RegEx to match a string only if it does'nt have a particular string within it Regular expressions forum covering PCRE and POSIX techniques, practices, and standards. Regular expressions help shorten coding time by providing the ability to compact many lines of code into one string.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

January 27th, 2009, 02:58 AM
|
|
Registered User
|
|
Join Date: Jan 2009
Posts: 3
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 !!
|

January 27th, 2009, 05:02 PM
|
 |
Still alive
|
|
Join Date: Mar 2007
Location: Washington, USA
|
|
Using a regular expression to do this is silly.
|

January 28th, 2009, 08:38 AM
|
|
|
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:
|

February 6th, 2009, 02:44 AM
|
|
Registered User
|
|
Join Date: Jan 2009
Posts: 3
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
|

February 6th, 2009, 03:47 AM
|
|
|
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.
|

February 8th, 2009, 10:51 PM
|
|
Registered User
|
|
Join Date: Jan 2009
Posts: 3
Time spent in forums: 36 m 21 sec
Reputation Power: 0
|
|
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 !!
|

February 9th, 2009, 01:02 AM
|
|
|
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.
|

April 2nd, 2009, 12:08 PM
|
|
|
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")
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|