Community

    • Login
    • Search
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search

    What this regex doing?

    Help wanted · · · – – – · · ·
    3
    3
    2397
    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.
    • Vasile Caraus
      Vasile Caraus last edited by Vasile Caraus

      hello. I’m just playing with regex, and I discover an interesting thing:

      Search : ^(.+)$
      Replace by: $1\2

      Can anyone tell me what this regex-replace doing? Because, something is happen, even if it is not show. Because when I press undo (ctr+z), I notice that something was executed, but don’t now what. Everything is the same, but undo is working…

      1 Reply Last reply Reply Quote 0
      • MAPJe71
        MAPJe71 last edited by

        Nothing useful IMO, depends on what you want to achieve.

        Here are some links to sites to get you started:
        https://learncodethehardway.org/regex/
        http://www.regular-expressions.info/
        http://www.rexegg.com/regex-quickstart.html
        http://regexlib.com/default.aspx
        https://regex101.com/

        1 Reply Last reply Reply Quote 1
        • guy038
          guy038 last edited by guy038

          Hello Vasile Caraus,

          In replacement regex, the contents of a nth subset of parentheses, defining a group, in the search regex, may be written in THREE different ways :

          • From \1 to \9 So, this syntax is acceptable if you don’t have more than 9 capturing groups, in the search regex

          • From $0 to $N In the syntax $N N may be a number, greater than 10. I didn’t try it, but I suppose that N can be up to 65535 !!. Just note that $0 represents the totality of the match, whereas $1 stands for group 1, $2 stands for group 2 and so on…

          • From ${0} to ${N}, with the SAME syntax as above. This said, what is the interest to surround the number by curly braces ? Do you guess it…

          Well just imagine that you want, for instance, to rewrite, in replacement, the contents of group1, but preceded and followed by the string 999. At first sight, you would simply write 999$1999. However, after replacement, you just obtain the string 999 !
          Because the regex engine thinks that you want to write the literal string 999, followed by the contents of group $1999. As, of course, this group does not exists in the search regex, it just replace the non-defined group $1999 by an empty string !

          Therefore, the solution consists to use the third and right syntax, giving the replacement regex 999${1}999


          So, Vasile Caraus, when you write $1\2, in replacement ( or also $1$2, \1$2 or \1\2 ) as you didn’t define a second group, in the search regex, this is just equivalent to the simple \1 or $1 syntax.

          And, as the group 1 (.+) represents all the standard characters of a NON-empty line, between the two assertions Beginning and End of line, you just replace any line by itself !!

          Then, it’s obvious that hitting CTRL-Z cancels each S/R, giving back the identical original lines :-))

          Best Regards,

          guy38

          1 Reply Last reply Reply Quote 1
          • First post
            Last post
          Copyright © 2014 NodeBB Forums | Contributors