4

I am trying to understand the regular expression given below:

"/login?(\\?.+)?"

I have gone through the Java docs, but I am not able to clearly understand the purpose of this expression.

I understand that it looks for a string that starts with /login, then after that what does the characters ?(\\?.+)? represent? Please help me in understanding this.

Chaitanya
  • 14,187
  • 31
  • 92
  • 131

4 Answers4

9

It optionally matches literal text ?some-text-here after /login. Also /login? makes last n optional:

It matches following inputs:

/logi
/login
/logi?something
/login?something

Regex Demo

anubhava
  • 664,788
  • 59
  • 469
  • 547
  • +1, I would add that it also matches `/login?something and my dog`... :) – zx81 Jun 26 '14 at 06:41
  • Thanks a lot anubhava, now I understood that `n?` makes `n` as optional. But I have a confusion on why we have double slash for `\\?`, also why we need the brackets `()` here. Also what is the use of the last character `?`. Please help me in understanding. – Chaitanya Jun 26 '14 at 06:46
  • The bracket is there to group. It can be used to ask the regex matcher what substring was matched by the subregex inside the bracket. – Santa Jun 26 '14 at 06:52
  • 1
    1. In Java we use double escape to escape any regex special symbol and since `?` means 0 or 1 in regex therefore it uses: `\\?` 2. `(\\?.+)` makes it a captured group. 3. `?` after `(\\?.+)` makes literal `?bla-bla` optional. – anubhava Jun 26 '14 at 06:59
  • Also if you open my suggested demo link there is a very nice **EXPLANATION** of this regex on top-right side. – anubhava Jun 26 '14 at 07:00
3
  • /login matches the literal characters /login
  • the ? makes the n optional
  • the (parentheses) capture the match inside to Group 1
  • /login
  • \\? matches a literal ?. In normal regex it would be \?, but in the Java string the backslash has to be escaped with another backslash
  • the .+ matches one or more characters, which can include spaces and my dog!

Sample Matches

/logi
/login
/login?a
/logi?an
/logi?and my dog

General Answers to "What Does this Regex Mean?

  1. You can use a tool such as See regex101 to play with a regex. The right pane explains it token by token.
  2. There are several explaining tools based on the same original Perl library, such as this one, on which one of the answers is based.
  3. The ultimate answer can be found in Mastering Regular Expressions, 3rd Ed. and several excellent online tutorials, including the regex FAQ on this site.
Community
  • 1
  • 1
zx81
  • 38,175
  • 8
  • 76
  • 97
1

It matches /logi followed by an optional n (? = the previous element is optional) followed by an optional group (() limit a group) starting with ? (due to the double \ which escape the ?) and followed by one or more characters (.+, . = arbitrary character, + = one or more).

Smutje
  • 16,300
  • 3
  • 20
  • 36
-1

logi is the static content of the expression.

  • n? means n occurs never or once
  • ()? means the expression in () occurs never or once
  • \\? means \ occurs never or once
  • .+ means any character(except new line) occurs once or more
Artjom B.
  • 58,311
  • 24
  • 111
  • 196
myxlptlk
  • 139
  • 3