Link to home
Create AccountLog in
Avatar of Thomas PAIK
Thomas PAIK

asked on

Regex.match to trim leading and trailing whitespaces while preserving line count

https://regex101.com/r/C0nQGn/1

Please kindly provide a line-by-line trimming needle for a regex.Matches scheme (not regex.Replace) such that the matched results preserve the original line count (which is 22 lines), i.e. regex.Matches(text,needle,RegexOptions.Multiline).count = 22

Thank you.
Avatar of Zvonko
Zvonko
Flag of North Macedonia image

Like this:
/(^\s*|\s*$)/gmi


Avatar of Thomas PAIK
Thomas PAIK

ASKER

The regex.Match result should be something like that of below (please kindly refer  to the SUBSTITUTION section of the link), but NOT through using regex.Replace:

https://regex101.com/r/lSRoEn/1
Ok:
/^\s*(\S(.*\S)?)\s*$/gmi


As mentioned above, line count should also be preserved.

regex.Matches(text,needle,RegexOptions.Multiline).count =22

ASKER CERTIFIED SOLUTION
Avatar of Bill Prew
Bill Prew

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account