View Single Post
Old 09-28-2021, 09:20 PM   #9
Ted Friesen
Enthusiast
Ted Friesen began at the beginning.
 
Posts: 42
Karma: 10
Join Date: May 2016
Device: Kindle
Quote:
Originally Posted by chaley View Post
"needs to explain" is a bit strong...

Your expression needs to match the entire string. Anything not matched is left behind and included in the result. To that end you should understand the difference between greedy and non-greedy operators, and the semantics of anchors. The expression "(.*)" matches as much as possible to succeed, including nothing, leaving behind any unmatched text. The expression "(.*?)" matches as little as possible to succeed, including nothing, leaving behind any unmatched text. Adding the "\d+" forces the match to find at least one number.

If you add anchors then you can be sure that you match the entire line. Without spending a lot of time looking at the specifics, it seem that the anchored expression
Code:
^(.*?)(\#)(\d+)\s(.*?)$
does what you want, as does
Code:
^(.*?)(\#)(\d+)\s(.*)$
Chaley,

Let me rephrase. Please strike "someone needs to explain" and substitute "please help me understand" as you have graciously done. Thanks. I'll work with your suggestion.
Ted Friesen is offline   Reply With Quote