简体   繁体   中英

Regex to select a pattern of SubString

What is the syntax for finding and selecting part of a string in Regx C#?

The string could be:

string tdInnerHtml = "<strong> You gained  230 Points </strong> 
                      there is going to be more text and some html code part of this       
                      string <a href=http://google.com>Google it here </a>";

// I want to extract 230 from this string using Regx. 
// The digits (230) vary for each tdInnerHtml. 
// So code would be to look for digits, followed by space, ending with Points

If the space and the </strong> tag are consistent, you can use the following to get the match there, and will work with strings like: " Pints are between 230-240 Points and You gained 230 Points "

        var match = Regex.Match(tdInnerHtml, @"(?<pts>\d+) Points ?</strong>");
        if (match.Success) {
            int points = Convert.ToInt32(match.Groups["pts"].Value);
            Console.WriteLine("Points: {0}", points);
        }

I think your regex pattern might be \\b[0-9]+\\b \\bPoints\\b .

You might test this at regexpal .

As long as you're only going for a set of numbers followed by the text Points , Regex can work:

Match match = Regex.Match(tdInnerHtml, @"(?<![\d-])(\d+) Points");
if (match.Success){
  // fetch result
  String pointsString = match.Groups[1].Value;

  // optional: parse to integer
  Int32 points;
  if (Int32.TryParse(pointsString, out points)){
    // you now have an integer value
  }
}

However, if this is in any way related to where the information resides on the page, formatting its surrounded by, or anything else HTML related--heed others' warnings and use an HTML parser.

The regex is very easy, \\d+ Points . Here it is in C#, with a named group capture:

        var match = Regex.Match(tdInnerHtml, "(?<pts>\d+) Points");
        if (match.Success) {
            int points = (int)match.Groups["pts"].Value;
            // do something..
        }
string test = "<strong> You gained 230 Points </strong>";
string pattern = @"(\d+)\sPoints";
Regex regex = new Regex(pattern);
Match match = regex.Match(test);
string result = match.Success ? match.Groups[1].Value : "";

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM