regextextmateoniguruma

Explain this Regular Expression please


Regular Expressions are a complete void for me. I'm dealing with one right now in TextMate that does what I want it to do...but I don't know WHY it does what I want it to do.

/[[:alpha:]]+|( )/(?1::$0)/g

This is used in a TextMate snippet and what it does is takes a Label and outputs it as an id name. So if I type "First Name" in the first spot, this outputs "FirstName". Previously it looked like this:

/[[:alpha:]]+|( )/(?1:_:/L$0)/g (it might have been \L instead)

This would turn "First Name" into "first_name". So I get that the underscore adds an underscore for a space, and that the /L lowercases everything...but I can't figure out what the rest of it does or why.

Someone care to explain it piece by piece?

EDIT

Here is the actual snippet in question:

<column header="$1"><xmod:field name="${2:${1/[[:alpha:]]+|( )/(?1::$0)/g}}"/></column>

Solution

  • This regular expression (regex) format is basically:

     /matchthis/replacewiththis/settings
    

    The "g" setting at the end means do a global replace, rather than just restricting the regex to a particular line or selection.

    Breaking it down further...

      [[:alpha:]]+|( )
    

    That matches an alpha numeric character (held in parameter $0), or optionally a space (held in matching parameter $1).

      (?1::$0)
    

    As Roger says, the ? indicates this part is a conditional. If a match was found in parameter $1 then it is replaced with the stuff between the colons :: - in this case nothing. If nothing is in $1 then the match is replaced with the contents of $0, i.e. any alphanumeric character that is not a space is output unchanged.

    This explains why the spaces are removed in the first example, and the spaces get replaced with underscores in your second example.

    In the second expression the \L is used to lowercase the text.

    The extra question in the comment was how to run this expression outside of TextMate. Using vi as an example, I would break it into multiple steps:

    :0,$s/ //g
    :0,$s/\u/\L\0/g
    

    The first part of the above commands tells vi to run a substitution starting on line 0 and ending at the end of the file (that's what $ means).

    The rest of the expression uses the same sorts of rules as explained above, although some of the notation in vi is a bit custom - see this reference webpage.