1

I am just starting to learn regular expression module within python and I am being asked to explain an interesting regular expression sequence.

/^[a-z0-9_-]{3,16}$/

I can explain the codes within the two forward slash that search for a username that is alphanumeric including hyphen and underscore and has at least 3 and at most 16 digits or characters.

Now my question is, what does it mean by the two forward slashes? I tried the web and it seems that most tutorial has an explanation for backward slash but not forward slash. Please advise. Thanks.

yamixpcool
  • 111
  • 1
  • 3
  • 6

3 Answers3

3

The forward slashes are used as separators. They're only used in some flavors (Perl and JavaScript, for example), and can usually be changed to the delimiter if your choice. Changing the delimiter will change what (if anything) needs to be escaped.

See this sed statement with a regex I wrote earlier today for a different question:

sed -E 's/OldUtility.getList.([^)]*)\)([\)]*)/\1\2.getList()/g'

In this case:

  • s for substitute
  • / the first slash
  • The regex. If the regex needed a / it would need to be escaped. If you have enough /s that need to be escaped, it's good to switch to a different delimiter, if possible.
  • / the second slash
  • Then, there's the substitution: \1\2.getList()
  • / the third and last slash
  • Lastly, there's the modifier: g for global.
Graham
  • 7,431
  • 18
  • 59
  • 84
Laurel
  • 5,965
  • 14
  • 31
  • 57
  • we should not be answering these question, but flagging them instead... – omarjmh Apr 06 '16 at 02:05
  • 1
    @Omarjmh flagging for what reason? – Laurel Apr 06 '16 at 02:08
  • Bc its a duplicate – omarjmh Apr 06 '16 at 02:10
  • 1
    @Omarjmh Where's the dupe? Quality dictates which version gets closed. The question you linked to is JS specific. If you read my answer or another one, this is asks in general, as the behavior is shared between many flavors. I don't think it's a close enough dupe. – Laurel Apr 06 '16 at 02:15
  • fair enough laurel, I will undo the downvote , can you edit so I can take it back? You did enough work here, I acted hastily... lets let the mods decide on the dupe, my bad – omarjmh Apr 06 '16 at 02:22
  • 2
    @Omarjmh Thanks. I think this question should be at least marginally useful for future people, unlike many of the "i can haz regex" questions I see (and sometimes answer). – Laurel Apr 06 '16 at 02:30
1

The slashes represent the start and end of your regex. This is typical of how Perl expresses its regexes:

/<my_regex_here>/

In Perl, you can specify various options such as:

s/<my_regex>/<replaceWith>/

Perl is of course a language designed specifically for regexes, so it's common to see people talk about regexes using Perl-like syntax.

Mateen Ulhaq
  • 24,552
  • 19
  • 101
  • 135
0

Forward slash is just a divider which delimits the start and end of a regular expression. The reason that it's forward slash and not some other character is mainly convention.

For example, you can define a regular expression in vim like this, with a question mark instead of the conventional slash:

:s?[a-z0-9_-]??g

fandrianto
  • 31
  • 3