I have Text as below
1. This is 678 897 999
not a text which I want
2. This is 678 897 879
I have applied regex as
This\s*is\s*(\s+\d+){1,}(?: ){0,}[\r\n]+
Now what I want is match a string which does not have not next to the match string. I don't want the regex to match first string.
EDIT
Suppose I have 2 string as above and I applied regex then I found 2 match
This is 678 897 999
This is 678 897 879
Upto this all is perfect but now I want regex which does not contain not(in first string) , I want to match only 2nd string.
This\s*is\s*(\s+\d+){1,}(?: ){0,}(?:[\r\n]+|$)(?!not)
Just add lookahead
.See demo.
I want regex which does not contain not(in first string), I want to match only 2nd string.
That means you should check if the This is...
pattern is not followed by newline sequence + spaces* + not
as a whole word with backtracking disabled . We can disable backtracking using atomic group in .NET:
(?>This\s+is(?:\s+\d+)+ *)(?![\r\n]+\p{Zs}*not\b)
See the regex demo
Part 1 of the regex This\\s+is(?:\\s+\\d+)+ *
matches This is
followed with one or more sequences of one or more whitespaces followed with one or more digits, then followed with zero or more spaces. The (?>...)
prevent backtracking inside this part of the pattern. The lookahead (?![\\r\\n]+\\p{Zs}*not\\b)
fails the match if the previously matched text is followed with the whitespaces followed with a whole word not
(where \\b
stands for a word boundary).
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.