Multiple RegEx negation matching - c#

I have the following RegEx patterns:
"[0-9]{4,5}\.FU|[0-9]{4,5}\.NG|[0-9]{4,5}\.SP|[0-9]{4,5}\.T|JGB[A-Z][0-9]|JNI[A-Z][0-9]|JN4F[A-Z][0-9]|JNM[A-Z][0-9]|JTI[A-Z][0-9]|JTM[A-Z][0-9]|NIY[A-Z][0-9]|SSI[A-Z][0-9]|JNI[A-Z][0-9]-[A-Z][0-9]|JTI[A-Z][0-9]-[A-Z][0-9]" ===> matches 8411.T or JNID8
"[0-9]{4,5}\.HK|HSI[A-Z][0-9]|HMH[A-Z][0-9]|HCEI[A-Z][0-9]|HCEI[A-Z][0-9]-[A-Z][0-9]" ==> matches 9345.HK or HCEIU9-A9
".*\.SI|SFC[A-Z][0-9]" ==> matches 8345.SI or SFCX8
How can I obtain a RegEx from the negation of these patterns?
I want to match strings that match neither of these 3 patterns:
e.g. I want to match 8411.ABC, but not any of the aforementioned strings (8411.T, HCEIU-A9, 8345.SI, etc.).
I've tried (just to exclude 2 and 3 for instance, but it doesn't work):
^(?!((.*\.SI|SFC[A-Z][0-9])|([0-9]{4,5}\.HK|HSI[A-Z][0-9]|HMH[A-Z][0-9]|HCEI[A-Z][0-9]|HCEI[A-Z][0-9]-[A-Z][0-9])))

The main idea here is to place the patterns into (?!.*<pattern>) negative lookaheads anchored at the start of the string (^). The difficulty here is that you patterns contain unanchored alternations, and if not grouped, the .* before the patterns will only refer to the first alternative (i.e. all the subsequent alternatives will only be negated at the start of the string.
Thus, your pattern formula is ^(?!.*(?:<PATTERN1>))(?!.*(?:<PATTERN2>))(?!.*(?:<PATTERN3>)). Note that .+ or .* at the end is optional if you need to just get a boolean result. Note that in the last pattern, you need to remove the .* in the first alternative, it won't make sense to use .*.*.
Use
^(?!.*(?:[0-9]{4,5}\.FU|[0-9]{4,5}\.NG|[0-9]{4,5}\.SP|[0-9]{4,5}\.T|JGB[A-Z][0-9]|JNI[A-Z][0-9]|JN4F[A-Z][0-9]|JNM[A-Z][0-9]|JTI[A-Z][0-9]|JTM[A-Z][0-9]|NIY[A-Z][0-9]|SSI[A-Z][0-9]|JNI[A-Z][0-9]-[A-Z][0-9]|JTI[A-Z][0-9]-[A-Z][0-9]))(?!.*(?:[0-9]{4,5}\.HK|HSI[A-Z][0-9]|HMH[A-Z][0-9]|HCEI[A-Z][0-9]|HCEI[A-Z][0-9]-[A-Z][0-9]))(?!.*(?:\.SI|SFC[A-Z][0-9])).+
See the regex demo.
You may also contract the formula to ^(?!.*(?:<PATTERN1>|<PATTERN2>|<PATTERN3>)):
^(?!.*(?:[0-9]{4,5}\.FU|[0-9]{4,5}\.NG|[0-9]{4,5}\.SP|[0-9]{4,5}\.T|JGB[A-Z][0-9]|JNI[A-Z][0-9]|JN4F[A-Z][0-9]|JNM[A-Z][0-9]|JTI[A-Z][0-9]|JTM[A-Z][0-9]|NIY[A-Z][0-9]|SSI[A-Z][0-9]|JNI[A-Z][0-9]-[A-Z][0-9]|JTI[A-Z][0-9]-[A-Z][0-9]|[0-9]{4,5}\.HK|HSI[A-Z][0-9]|HMH[A-Z][0-9]|HCEI[A-Z][0-9]|HCEI[A-Z][0-9]-[A-Z][0-9]|\.SI|SFC[A-Z][0-9])).+
See another regex demo.

Related

Stop When <br> is Encountered In C# RegEx [duplicate]

My regex pattern looks something like
<xxxx location="file path/level1/level2" xxxx some="xxx">
I am only interested in the part in quotes assigned to location. Shouldn't it be as easy as below without the greedy switch?
/.*location="(.*)".*/
Does not seem to work.
You need to make your regular expression lazy/non-greedy, because by default, "(.*)" will match all of "file path/level1/level2" xxx some="xxx".
Instead you can make your dot-star non-greedy, which will make it match as few characters as possible:
/location="(.*?)"/
Adding a ? on a quantifier (?, * or +) makes it non-greedy.
Note: this is only available in regex engines which implement the Perl 5 extensions (Java, Ruby, Python, etc) but not in "traditional" regex engines (including Awk, sed, grep without -P, etc.).
location="(.*)" will match from the " after location= until the " after some="xxx unless you make it non-greedy.
So you either need .*? (i.e. make it non-greedy by adding ?) or better replace .* with [^"]*.
[^"] Matches any character except for a " <quotation-mark>
More generic: [^abc] - Matches any character except for an a, b or c
How about
.*location="([^"]*)".*
This avoids the unlimited search with .* and will match exactly to the first quote.
Use non-greedy matching, if your engine supports it. Add the ? inside the capture.
/location="(.*?)"/
Use of Lazy quantifiers ? with no global flag is the answer.
Eg,
If you had global flag /g then, it would have matched all the lowest length matches as below.
Here's another way.
Here's the one you want. This is lazy [\s\S]*?
The first item:
[\s\S]*?(?:location="[^"]*")[\s\S]* Replace with: $1
Explaination: https://regex101.com/r/ZcqcUm/2
For completeness, this gets the last one. This is greedy [\s\S]*
The last item:[\s\S]*(?:location="([^"]*)")[\s\S]*
Replace with: $1
Explaination: https://regex101.com/r/LXSPDp/3
There's only 1 difference between these two regular expressions and that is the ?
The other answers here fail to spell out a full solution for regex versions which don't support non-greedy matching. The greedy quantifiers (.*?, .+? etc) are a Perl 5 extension which isn't supported in traditional regular expressions.
If your stopping condition is a single character, the solution is easy; instead of
a(.*?)b
you can match
a[^ab]*b
i.e specify a character class which excludes the starting and ending delimiiters.
In the more general case, you can painstakingly construct an expression like
start(|[^e]|e(|[^n]|n(|[^d])))end
to capture a match between start and the first occurrence of end. Notice how the subexpression with nested parentheses spells out a number of alternatives which between them allow e only if it isn't followed by nd and so forth, and also take care to cover the empty string as one alternative which doesn't match whatever is disallowed at that particular point.
Of course, the correct approach in most cases is to use a proper parser for the format you are trying to parse, but sometimes, maybe one isn't available, or maybe the specialized tool you are using is insisting on a regular expression and nothing else.
Because you are using quantified subpattern and as descried in Perl Doc,
By default, a quantified subpattern is "greedy", that is, it will
match as many times as possible (given a particular starting location)
while still allowing the rest of the pattern to match. If you want it
to match the minimum number of times possible, follow the quantifier
with a "?" . Note that the meanings don't change, just the
"greediness":
*? //Match 0 or more times, not greedily (minimum matches)
+? //Match 1 or more times, not greedily
Thus, to allow your quantified pattern to make minimum match, follow it by ? :
/location="(.*?)"/
import regex
text = 'ask her to call Mary back when she comes back'
p = r'(?i)(?s)call(.*?)back'
for match in regex.finditer(p, str(text)):
print (match.group(1))
Output:
Mary

Regex - Match pattern A unless it matches pattern B

I'm trying to excluded some very specific routes from my MVC project.
More specifically I want to ignore all calls to .ashx pages, unless they match a certain pattern.
(?<!invoices\/(order|membership)\/(\d{5,})-([a-f0-9]{8}))\.ashx
This is the pattern I came up with, but since you can't use quantifiers in a negated lookbehind, it's not working.
Any ideas as to how I can achieve this so I can ignore my routes correctly with a call like this:
routes.Ignore("{*handlers}", new { handlers = "(?<!invoices/(order|membership)/(\\d{5,})-([a-f0-9]{8}))\\.ashx" });
.NET regex flavor does support infinite-width lookbehinds, so the only issue with your pattern is the double backslashes. Use \d instead of \\d and \. instead of \\., or just work around that with character classes [0-9] (a digit) and [.] (a literal dot):
(?<!invoices/(order|membership)/[0-9]{5,}-[a-f0-9]{8})[.]ashx
^^^^^ ^^^
You can also get rid of the lookbehind, and use a lookahead anchored at the start:
^(?!.*invoices/(order|membership)/[0-9]{5,}-[a-f0-9]{8}).*[.]ashx.
The (?!.*invoices/(order|membership)/[0-9]{5,}-[a-f0-9]{8}) negative lookahead will fail the match if a string contains (remove the first .* to make it starts with) the invoices/(order|membership)/[0-9]{5,}-[a-f0-9]{8} pattern.

Difference between the Regex expressions in dotnet

what is the difference between the two regex
new Regex(#"(([[[{""]))", RegexOptions.Compiled)
and
new Regex(#"(^[[[{""])", RegexOptions.Compiled)
I've used the both regex but can't find the difference. it's almost match similar things.
The regex patterns are not well written because
There are duplicate characters in character classes (thus redundant)
The first regex contains duplicate capture group on the whole pattern.
The first regex - (([[[{""])) - matches 1 character, either a [, a {, or a ", and captures it into Group 1 and Group 2. See demo. It is equal to
[[{"]
Demo
The second regex - (^[[[{""]) - only matches the same characters as the pattern above, but at the beginning of a string (if RegexOptions.Multiline is not set), or the beginning of a line (if that option is set). See demo. It is equal to
^[[{"]
See demo
You will access the matched characters using Regex.Match(s).Value.
More about anchors
Aslo see Caret ^: Beginning of String (or Line)

Regex to match two or more consecutive characters

Using regular expressions I want to match a word which
starts with a letter
has english alpahbets
numbers, period(.), hyphen(-), underscore(_)
should not have two or more consecutive periods or hyphens or underscores
can have multiple periods or hyphens or underscore
For example,
flin..stones or flin__stones or flin--stones
are not allowed.
fl_i_stones or fli_st.ones or flin.stones or flinstones
is allowed .
So far My regular expression is ^[a-zA-Z][a-zA-Z\d._-]+$
So My question is how to do it using regular expression
You can use a lookahead and a backreference to solve this. But note that right now you are requiring at least 2 characters. The starting letter and another one (due to the +). You probably want to make that + and * so that the second character class can be repeated 0 or more times:
^(?!.*(.)\1)[a-zA-Z][a-zA-Z\d._-]*$
How does the lookahead work? Firstly, it's a negative lookahead. If the pattern inside finds a match, the lookahead causes the entire pattern to fail and vice-versa. So we can have a pattern inside that matches if we do have two consecutive characters. First, we look for an arbitrary position in the string (.*), then we match single (arbitrary) character (.) and capture it with the parentheses. Hence, that one character goes into capturing group 1. And then we require this capturing group to be followed by itself (referencing it with \1). So the inner pattern will try at every single position in the string (due to backtracking) whether there is a character that is followed by itself. If these two consecutive characters are found, the pattern will fail. If they cannot be found, the engine jumps back to where the lookahead started (the beginning of the string) and continue with matching the actual pattern.
Alternatively you can split this up into two separate checks. One for valid characters and the starting letter:
^[a-zA-Z][a-zA-Z\d._-]*$
And one for the consecutive characters (where you can invert the match result):
(.)\1
This would greatly increase the readability of your code (because it's less obscure than that lookahead) and it would also allow you to detect the actual problem in pattern and return an appropriate and helpful error message.

Extending regular expression syntax to say 'does not contain text XYZ'

I have an app where users can specify regular expressions in a number of places. These are used while running the app to check if text (e.g. URLs and HTML) matches the regexes. Often the users want to be able to say where the text matches ABC and does not match XYZ. To make it easy for them to do this I am thinking of extending regular expression syntax within my app with a way to say 'and does not contain pattern'. Any suggestions on a good way to do this?
My app is written in C# .NET 3.5.
My plan (before I got the awesome answers to this question...)
Currently I'm thinking of using the ¬ character: anything before the ¬ character is a normal regular expression, anything after the ¬ character is a regular expression that can not match in the text to be tested.
So I might use some regexes like this (contrived) example:
on (this|that|these) day(s)?¬(every|all) day(s) ?
Which for example would match 'on this day the man said...' but would not match 'on this day and every day after there will be ...'.
In my code that processes the regex I'll simply split out the two parts of the regex and process them separately, e.g.:
public bool IsMatchExtended(string textToTest, string extendedRegex)
{
int notPosition = extendedRegex.IndexOf('¬');
// Just a normal regex:
if (notPosition==-1)
return Regex.IsMatch(textToTest, extendedRegex);
// Use a positive (normal) regex and a negative one
string positiveRegex = extendedRegex.Substring(0, notPosition);
string negativeRegex = extendedRegex.Substring(notPosition + 1, extendedRegex.Length - notPosition - 1);
return Regex.IsMatch(textToTest, positiveRegex) && !Regex.IsMatch(textToTest, negativeRegex);
}
Any suggestions on a better way to implement such an extension? I'd need to be slightly cleverer about splitting the string on the ¬ character to allow for it to be escaped, so wouldn't just use the simple Substring() splitting above. Anything else to consider?
Alternative plan
In writing this question I also came across this answer which suggests using something like this:
^(?=(?:(?!negative pattern).)*$).*?positive pattern
So I could just advise people to use a pattern like, instead of my original plan, when they want to NOT match certain text.
Would that do the equivalent of my original plan? I think it's quite an expensive way to do it peformance-wise, and since I'm sometimes parsing large html documents this might be an issue, whereas I suppose my original plan would be more performant. Any thoughts (besides the obvious: 'try both and measure them!')?
Possibly pertinent for performance: sometimes there will be several 'words' or a more complex regex that can not be in the text, like (every|all) in my example above but with a few more variations.
Why!?
I know my original approach seems weird, e.g. why not just have two regexes!? But in my particular application administrators provide the regular expressions and it would be rather difficult to give them the ability to provide two regular expressions everywhere they can currently provide one. Much easier in this case to have a syntax for NOT - just trust me on that point.
I have an app that lets administrators define regular expressions at various configuration points. The regular expressions are just used to check if text or URLs match a certain pattern; replacements aren't made and capture groups aren't used. However, often they would like to specify a pattern that says 'where ABC is not in the text'. It's notoriously difficult to do NOT matching in regular expressions, so the usual way is to have two regular expressions: one to specify a pattern that must be matched and one to specify a pattern that must not be matched. If the first is matched and the second is not then the text does match. In my application it would be a lot of work to add the ability to have a second regular expression at each place users can provide one now, so I would like to extend regular expression syntax with a way to say 'and does not contain
pattern'.
You don't need to introduce a new symbol. There already is support for what you need in most regex engines. It's just a matter of learning it and applying it.
You have concerns about performance, but have you tested it? Have you measured and demonstrated those performance problems? It will probably be just fine.
Regex works for many many people, in many many different scenarios. It probably fits your requirements, too.
Also, the complicated regex you found on the other SO question, can be simplified. There are simple expressions for negative and positive lookaheads and lookbehinds.
?! ?<! ?= ?<=
Some examples
Suppose the sample text is <tr valign='top'><td>Albatross</td></tr>
Given the following regex's, these are the results you will see:
tr - match
td - match
^td - no match
^tr - no match
^<tr - match
^<tr>.*</tr> - no match
^<tr.*>.*</tr> - match
^<tr.*>.*</tr>(?<tr>) - match
^<tr.*>.*</tr>(?<!tr>) - no match
^<tr.*>.*</tr>(?<!Albatross) - match
^<tr.*>.*</tr>(?<!.*Albatross.*) - no match
^(?!.*Albatross.*)<tr.*>.*</tr> - no match
Explanations
The first two match because the regex can apply anywhere in the sample (or test) string. The second two do not match, because the ^ says "start at the beginning", and the test string does not begin with td or tr - it starts with a left angle bracket.
The fifth example matches because the test string starts with <tr.
The sixth does not, because it wants the sample string to begin with <tr>, with a closing angle bracket immediately following the tr, but in the actual test string, the opening tr includes the valign attribute, so what follows tr is a space. The 7th regex shows how to allow the space and the attribute with wildcards.
The 8th regex applies a positive lookbehind assertion to the end of the regex, using ?<. It says, match the entire regex only if what immediately precedes the cursor in the test string, matches what's in the parens, following the ?<. In this case, what follows that is tr>. After evaluating ``^.*, the cursor in the test string is positioned at the end of the test string. Therefore, thetr>` is matched against the end of the test string, which evaluates to TRUE. Therefore the positive lookbehind evaluates to true, therefore the overall regex matches.
The ninth example shows how to insert a negative lookbehind assertion, using ?<! . Basically it says "allow the regex to match if what's right behind the cursor at this point, does not match what follows ?<! in the parens, which in this case is tr>. The bit of regex preceding the assertion, ^<tr.*>.*</tr> matches up to and including the end of the string. Because the pattern tr> does match the end of the string. But this is a negative assertion, therefore it evaluates to FALSE, which means the 9th example is NOT a match.
The tenth example uses another negative lookbehind assertion. Basically it says "allow the regex to match if what's right behind the cursor at this point, does not match what's in the parens, in this case Albatross. The bit of regex preceding the assertion, ^<tr.*>.*</tr> matches up to and including the end of the string. Checking "Albatross" against the end of the string yields a negative match, because the test string ends in </tr>. Because the pattern inside the parens of the negative lookbehind does NOT match, that means the negative lookbehind evaluates to TRUE, which means the 10th example is a match.
The 11th example extends the negative lookbehind to include wildcards; in english the result of the negative lookbehind is "only match if the preceding string does not include the word Albatross". In this case the test string DOES include the word, the negative lookbehind evaluates to FALSE, and the 11th regex does not match.
The 12th example uses a negative lookahead assertion. Like lookbehinds, lookaheads are zero-width - they do not move the cursor within the test string for the purposes of string matching. The lookahead in this case, rejects the string right away, because .*Albatross.* matches; because it is a negative lookahead, it evaluates to FALSE, which mean the overall regex fails to match, which means evaluation of the regex against the test string stops there.
example 12 always evaluates to the same boolean value as example 11, but it behaves differently at runtime. In ex 12, the negative check is performed first, at stops immediately. In ex 11, the full regex is applied, and evaluates to TRUE, before the lookbehind assertion is checked. So you can see that there may be performance differences when comparing lookaheads and lookbehinds. Which one is right for you depends on what you are matching on, and the relative complexity of the "positive match" pattern and the "negative match" pattern.
For more on this stuff, read up at http://www.regular-expressions.info/
Or get a regex evaluator tool and try out some tests.
like this tool:
source and binary
You can easily accomplish your objectives using a single regex. Here is an example which demonstrates one way to do it. This regex matches a string containing "cat" AND "lion" AND "tiger", but does NOT contain "dog" OR "wolf" OR "hyena":
if (Regex.IsMatch(text, #"
# Match string containing all of one set of words but none of another.
^ # anchor to start of string.
# Positive look ahead assertions for required substrings.
(?=.*? cat ) # Assert string has: 'cat'.
(?=.*? lion ) # Assert string has: 'lion'.
(?=.*? tiger ) # Assert string has: 'tiger'.
# Negative look ahead assertions for not-allowed substrings.
(?!.*? dog ) # Assert string does not have: 'dog'.
(?!.*? wolf ) # Assert string does not have: 'wolf'.
(?!.*? hyena ) # Assert string does not have: 'hyena'.
",
RegexOptions.Singleline | RegexOptions.IgnoreCase |
RegexOptions.IgnorePatternWhitespace)) {
// Successful match
} else {
// Match attempt failed
}
You can see the needed pattern. When assembling the regex, be sure to run each of the user provided sub-strings through the Regex.escape() method to escape any metacharacters it may contain (i.e. (, ), | etc). Also, the above regex is written in free-spacing mode for readability. Your production regex should NOT use this mode, otherwise whitespace within the user substrings would be ignored.
You may want to add \b word boundaries before and after each "word" in each assertion if the substrings consist of only real words.
Note also that the negative assertion can be made a bit more efficient using the following alternative syntax:
(?!.*?(?:dog|wolf|hyena))

Categories