Regex to match file and version number
-
Greetings All:
I am having trouble getting the following regex to work
\w+.*?
It will only grab the file name not file name and version of:
bubba 1.2.11I only get “bubba” back
I can add the eol character $ and get all but the last line which may not have a CRLF. I need to be able to match this type of string.What am I doing wrong?
Appreciated your time looking at this.
Pat -
Hey Pat
The regex \w+.*? seems quite wide - it will match “any set of one or more word characters (equal to [a-zA-Z0-9_]”
Are you using a global switch? e.g. /\w+.*?/g (as a javascript regex object)
Without it, it will only run the match once, stopping after matching bubbaAlso, the ? makes the + lazy, so it will only match as little as needed - remove the ?
e.g. /\w+.*/g
(it should be returning the file name and version now and, depending on the text being matched, a lot of other stuff)A white space character (e.g. /\w+\s./g ) or space (/\w+\x20./g) character after \w would also limit the matches.
An end anchor $ does not require CRLF. In fact /\w+\x20.$/g would only match the last line with the file name and version.
Add a multi-line switch (e.g. /\w+\x20.$/gm) if there is only one match per lineFinally, we probably need to limit .* to match only numbers and periods
e.g. /\w+\x20[0-9.]+$/gmWhen I run document.body.innerText.match(/\w+\x20[0-9.]+$/gm) against this page in the console I get
(2) [“bubba 1.2.11”, “POSTS 66”]Check out [Filename and Version on RegEx101.com - a great resource] (https://regex101.com/r/a0fnej/5)