vendredi 27 février 2015

Regex - capture optional surrounding characters

I'm not that savvy in regex, so I'm not sure how to achieve the following thing:


I'd like to capture any arbitary string from an input that may or may not be surrounded by the '$' character. If a '$' character is present at the beginning of the string, the '$' character at the end must be present.


Currently I have



^\w+([_.-]\w+)*$


which roughly translates to:



  1. Arbitary word characters

  2. Beginning of capture group

  3. any character of '_', '.', '-'

  4. Before an optional \n


So valid matches would be:



test
test-5
test.1.3
test-alpha.2


Now I'd like to make this possible



$test$


But not...



$test (or test$)

Aucun commentaire:

Enregistrer un commentaire