|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| ||||||||||||||||||||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
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 !! |
|
#2
|
||||
|
||||
|
Using a regular expression to do this is silly.
Code:
^((?!BRANCH).)*$ |
|
#3
|
|||
|
|||
|
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).*$ |
|
#4
|
|||
|
|||
|
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 |
|
#5
|
||||
|
||||
|
Quote:
No problem. Quote:
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. |
|
#6
|
|||
|
|||
|
Quote:
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?)*$ Thanks again !! |
|
#7
|
|||
|
|||
|
Quote:
You're welcome. |
|
#8
|
|||
|
|||
|
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") |
![]() |
| Viewing: Dev Shed Forums > Programming Languages - More > Regex Programming > RegEx to match a string only if it does'nt have a particular string within it |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|