c# regex matches example

后端 未结 6 2122
耶瑟儿~
耶瑟儿~ 2020-12-01 11:46

I am trying to get values from the following text. How can this be done with Regex?

Input

Lorem ipsum dolor sit %download%#456 amet, consect

6条回答
  •  Happy的楠姐
    2020-12-01 12:09

    So you're trying to grab numeric values that are preceded by the token "%download%#"?

    Try this pattern:

    (?<=%download%#)\d+
    

    That should work. I don't think # or % are special characters in .NET Regex, but you'll have to either escape the backslash like \\ or use a verbatim string for the whole pattern:

    var regex = new Regex(@"(?<=%download%#)\d+");
    return regex.Matches(strInput);
    

    Tested here: http://rextester.com/BLYCC16700

    NOTE: The lookbehind assertion (?<=...) is important because you don't want to include %download%# in your results, only the numbers after it. However, your example appears to require it before each string you want to capture. The lookbehind group will make sure it's there in the input string, but won't include it in the returned results. More on lookaround assertions here.

提交回复
热议问题