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, consectetur adipiscing %download%#3434 elit. Duis non nunc nec mauris feugiat porttitor. Sed tincidunt blandit dui a viverra%download%#298. Aenean dapibus nisl %download%#893434 id nibh auctor vel tempor velit blandit.
Output
456
3434
298
893434
So you’re trying to grab numeric values that are preceded by the token “%download%#”?
Try this pattern:
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: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.