Manjay_TBAG
Manjay_TBAG

Reputation: 2245

Regex to identify numbers only if its followed by \r or \n it shoiuld not match

I am trying a regex which just identifies number but if its followed by new line or carriage return it should not match For example 3 is valid match

3\n

does not match

Desc - users copy a value and paste it but when user is copying if new line characters are copied we should invalidate it. \r and \n are mistakenly copied by user from editor.

\d+ is not doing the trick as it is not considering newline.

Upvotes: 1

Views: 136

Answers (1)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626758

\d+ is not doing the trick as it is not considering newline

That is true, one usually uses lookarounds to take into account context, or capturing groups coupled with backreferences in the replacement patterns (when replacing, as in this case).

Here is one way to match all digit chunks not followed with \r or \n:

\d+(?![\r\n\d])

See the regex demo.

Details

  • \d+ - 1+ digits (to only match ASCII digits, use [0-9] or pass RegexOptions.ECMAScript flag to the regex compile method)
  • (?![\r\n\d]) - a negative lookahead that fails the match if there is a digit, CR or LF symbols immediately to the right of the current location.

See a C# demo:

var s = "3 45 123\r 456\n 434554645\r\n 000";
var pattern = @"\d+(?![\r\n\d])";
var results = Regex.Matches(s, pattern)
            .Cast<Match>()
            .Select(m => m.Value);
Console.WriteLine("Result: {0}", string.Join(", ", results));
// => Result: 3, 45, 000

Another fancy regex is (?>\d+)(?![\r\n]), where the (?>\d+) is an atomic group allowing no backtracking into its pattern, and thus there is no longer need to check for a digit on the right.

Upvotes: 1

Related Questions