C # regex formula

For the next line, I need a regex to get the values ​​outside the double quotes, namely: 0.0

and 100.5

.

"VAL_ 344 PedalPos 0.0 \"% (0.0 ... 100.0)\" 100.5 \"Invalid - Undefined (100.5 ... 127.5)\";"

      

Using this rule Regex.Match(line, "\"\\s[0-9]+\\s\"")

, I get the same group, and the first value: 0.0

. I cannot figure out how to expand the search to include all of the following values.

With that part in mind [0-9]

, I think this only applies to integer values, I added a dot there [0-9.]

and that included all double numbers. Is it correct?

+3


source to share


3 answers


I suggest the following approach:

1) Remove all quoted strings,

2) Extract all numbers that are not preceded VAL_

.

var txt = "VAL_ 344 PedalPos 0.0 \"% (0.0 ... 100.0)\" 100.5 \"Invalid - Undefined (100.5 ... 127.5)\";";
txt = Regex.Replace(txt, @"""[^""]*""", string.Empty);
var results = Regex.Matches(txt, @"(?<!VAL_\s+)-?\b\d*\.?\d+\b");

      



Output:

enter image description here

Regex explanation:

  • "[^"]*"

    - match a quoted string
  • (?<!VAL_\s+)\b\d*\.?\d+\b

    :
    • (?<!VAL_\s+)

      - negative lookbehind to check if the number is not worth a constant VAL_

      and one or more spaces
    • \b\d*\.?\d+\b

      ... Match an integer word that is a floating number (slightly simplified, but it will work even with values .04

      ).
+1


source


Try it "\s(\d+\.?\d*)\s"

( string regex = "\"\\s(\\d+\\.?\\d*)\\s\"";

in code) and take the first group result.



+1


source


A more general approach that uses a single expression to get the numbers you want, as I understand it:

@"VAL_\s*\d+|""[^""]+""|(\d+(?:\.\d+)?)"

      

The way it works is that it actually matches the parts you don't want in the first place without doing much for it, and when it comes to the last part, it uses a capture group to get what you really need. Here's a snippet on how to use it:

string text = "VAL_ 344 PedalPos 0.0 \"% (0.0 ... 100.0)\" 100.5 \"Invalid - Undefined (100.5 ... 127.5)\";";
var re = new Regex(@"VAL_\s*\d+|""[^""]+""|(\d+(?:\.\d+)?)", RegexOptions.IgnoreCase);
var textmatches = re.Matches(text);
Console.WriteLine("Result:");
foreach (Match match in textmatches)
{
    Console.WriteLine(match.Groups[1].Value);
}

      

ideone demo

VAL_\s*\d+

matches VAL_

, followed by additional spaces and numbers for identifiers,

""[^""]+""

gets everything in double quotes,

(\d+(?:\.\d+)?)

and finally, this number. I used a basic one, so if you have more complex numbers (negatives, scientific format, etc.) you will have to change that accordingly.

+1


source







All Articles