I'm looking to extract:
50%
From a string that will have more or less this format:
The 50% is in here somewhere.
I'd also like to extract:
50%50%25%
From a string like this:
50% of 50% is 25%
Regex.Match()
seems the obvious contender. However, this involves checking if any matches were found (e.g. match.Success
), extracting the results from a particular index in the array, and/or the risk of addressing an out-of-bounds index.
Regex replace is generally simpler to apply. A single line does the job, including returning the resulting string. This is true for so many languages.
result = Regex.Replace(input, stuffWeDontLike, "")
Basically, I am looking for a regex filter - instead of entering the pattern to replace, I want to enter the pattern to retrieve.
percentages = Regex.Filter("50% of 50% is 25%", "[0-9]+\%")
Could we form a regex and invert the result, as if it were a selection? That would allow the use of regex replace. However, I could not find a way to easily invert a regex.
How can we achieve the desired result (or similar; a join or so seems acceptable) with very short and simple syntax, similar to regex replace?