Sergey Alikin
Sergey Alikin

Reputation: 161

Regular expression with full match only

I know that this hackneyed theme and I read related answers...
However, I will ask...
I have input string in next format:

3#0.01, 2#0.5, 1#-10, -2#~ 

So this is a list of values splitted by comma.
I can catch all values with next expression:

(([-+]?\d+)#([-+]?\d*\.?\d+|~))+ 

It's working fine.

But I want to have no matching if any mistake is presenting in input string, for example:

MISTAKE3#0.01, 2#0.5, 1#-10, -2#~AND_HERE_MISTAKE_TOO 

Unfortunately ^ and $ symbols are not helping here.
So my question: how can I stop matching if some part of input string is invalid.
Thanks.

Here is snippet: https://regex101.com/r/Xih0Qk/2

Upvotes: 2

Views: 2447

Answers (1)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626728

In .NET, you may use a regex with inifinite width lookbehinds (it is also supported in the latest ECMAScript 2018 powered JavaScript environments, if you need to port the same solution there). The regex will look like

(?<=^(?:[-+]?\d+#(?:[-+]?\d*\.?\d+|~),\s)*)[-+]?\d+#(?:[-+]?\d*\.?\d+|~)(?=(?:,\s[-+]?\d+#(?:[-+]?\d*\.?\d+|~))*$)

See the online regex demo

In code, it is easier to build the pattern from a variable:

var block = @"[-+]?\d+#(?:[-+]?\d*\.?\d+|~)";   // Block/unit pattern
var pattern = $@"(?<=^(?:{block},\s)*){block}(?=(?:,\s{block})*$)";
var results1 = Regex.Matches("3#0.01, 2#0.5, 1#-10, -2#~", pattern)
    .Cast<Match>().Select(x => x.Value);
if (results1.Count() > 0)
    Console.WriteLine(string.Join(", ", results1));
var results2 = Regex.Matches("MISTAKE3#0.01, 2#0.5, 1#-10, -2#~AND_HERE_MISTAKE_TOO", pattern)
    .Cast<Match>().Select(x => x.Value);
if (results2.Count() > 0)
    Console.WriteLine(string.Join(", ", results2));

See the C# demo online. Output (only the right string matched):

3#0.01, 2#0.5, 1#-10, -2#~

Pattern explanation

  • (?<=^(?:{block},\s)*) - a positive lookbehind that only matches a location immediately preceded with 0+ occurrences of a {block} pattern at the start of the string followed with a , and 1 whitespace
  • {block} - your block/unit pattern to match
  • (?=(?:,\s{block})*$) - a positive lookahead that matches a location immediately followed with 0+ occurrences of a ,, a whitespace and a {block} pattern up to the end of the string.

Upvotes: 2

Related Questions