Community
    • Login

    Regex to match file and version number

    Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
    2 Posts 2 Posters 2.2k Views
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • Pat SinclairP
      Pat Sinclair
      last edited by

      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.11

      I 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

      1 Reply Last reply Reply Quote 0
      • CyBrianC
        CyBrian
        last edited by

        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 bubba

        Also, 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 line

        Finally, we probably need to limit .* to match only numbers and periods
        e.g. /\w+\x20[0-9.]+$/gm

        When 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)

        1 Reply Last reply Reply Quote 0
        • First post
          Last post
        The Community of users of the Notepad++ text editor.
        Powered by NodeBB | Contributors