How to not include part of the regular expression

I am new to using regular expressions and I can understand how I am going to extract a specific number from a string.

Suppose a string is any number of spaces or random texts, and somewhere inside this value, "Value: $ 1,000.00."

To get this value, I am currently using this:

string value = Convert.ToString(Regex.Match(BodyContent, @"Value:[ \t]*\$?\d*(\.[0-9]{2})?", RegexOptions.Singleline));

So, the variable 'value' now has the value "Value: $ 1000.00" in it.

My question is, with Regex, is there a way to use the value "Value:" to find the value of a number, but store only the actual numerical value (ie 1000.00) in the variable "value"?

+3
source share
2 answers

Generally speaking, to accomplish something like this, you have at least 3 options:

  • Use lookarounds (?=...), (?<=...), so you can accurately align what you want to capture
    • Some languages ​​have limited lookbehinds support.
  • Use capture group (...)to capture specific lines
    • Near universal support in all options
  • You can also just take substringoff the match
    • Works well if the prefix / suffix length for shredding is a known constant

References


Examples

Given this test line:

i have 35 dogs, 16 cats and 10 elephants

These are matches for some regex patterns:

, :

  • (\d+) (cats|dogs) 2 (. rubular.com)
    • 1: 35 dogs
      • 1 35
      • 2 dogs
    • 2: 16 cats
      • 1 16
      • 2 cats

(. ideone.com):

var text = "Blah blah Value: $1000.00 and more stuff";
string value = Convert.ToString(
   Regex.Match(
     text,
     @"Value:[ \t]*\$?(\d*(\.[0-9]{2})?)",
     RegexOptions.Singleline
   ).Groups[1]
);

, :

  • .Groups[1] Match
+3

.NET Match, Groups:

Match m = Regex.Match(BodyContent, @"Value:[ \t]*\$?(?<amount>\d*(\.[0-9]{2})?)", RegexOptions.Singleline);
string value = null;

if (m.Success)
{
    value = m.Groups["amount"].Value;
}

(?<amount> ... ) , m.Groups.

+2

Source: https://habr.com/ru/post/1749268/


All Articles