Writing regular expressions may seem like black art, but it's actually quite simple; the most important step is to identify with surgical accuracy exactly what you are doing and do not want to match, and then say only what you mean (no more and no less).
One more tip: when using qualifiers * or + , especially with wildcards such as . , always remember that part of the regex may “skip” the part you wanted to match, the entire string. Often the easiest solution is to use a reluctant classifier like *? or +? . (The most common regular expression errors are those that make the regular expression match when you didn’t want it or more than you wanted.)
In this case, you want the "content of each line without extra spaces". This is not entirely accurate. What are "extra spaces"? Trailing and leading spaces? If so...
We will express this in perfectly precise, not double-digit terms. You basically have:
- White space (possibly blank)
- Or: a) Nothing. b) One character without spaces. c) The area that begins and ends with characters without spaces.
- White space (possibly blank)
Can you express this as a regular expression? Try to do this and post it here, then I will give you some feedback.
source share