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.
Regular Expressions

Avatar of undefined
Last Comment
Bill Prew

8/22/2022 - Mon
Zvonko

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


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
Zvonko

Ok:
/^\s*(\S(.*\S)?)\s*$/gmi


Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
Thomas PAIK

ASKER
As mentioned above, line count should also be preserved.

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

ASKER CERTIFIED SOLUTION
Bill Prew

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.