What the expression "/ ^ (? :( ?: cats? | dog):)? /" does?

7

The question is about the following regular expression:

/^(?:(?:gatos?|cachorro):)?/

In understanding I have the following:

  • Start: ^

  • ( ) : this is a grouping, right? type (gato|cachorro|etc..) ?

  • Why the first (? ? - indicates something like " If there is ( "?

  • At the end, it has a block () with ? . Is it something like the block exists?

asked by anonymous 14.08.2017 / 22:35

2 answers

14

Let's explain the regex:

  • /.../ - These / are used in Javascript to denote that what is inside them is a regex. So, the real regex is ^(?:(?:gatos?|cachorro):)? .

  • ^ - Beginning of the string. This means that whatever is found, has to be found at the beginning of the string, not in the middle of it.

  • (?: ... ) - This is a group without capture. It is used only for grouping subexpressions. Regex allow captures with ( ... ) , so you can extract parts of the text that gives match . Using this ?: disables capture when you are not interested in it or when it might mess up other parts in which you want the capture to take place. In your case, you have no interest in capturing parts, just the whole.

  • gatos?|cachorro - This can be gato or gatos or cachorro . s? means s may or may not appear. The | indicates alternatives. The (?: ... ) around is used to group this together so that | knows where the alternatives start and ends.

  • : (the last) - Means the character : itself.

  • The last ? - means that what is before (?: ... ) may or may not appear. If it does not appear, it is still given match.

In this way, there are only four strings that this regex recognizes and that should be docked at the beginning of the string. These are:

  • (empty)

  • gato:

  • gatos:

  • cachorro:

Note that the end of the string is not checked. Therefore, the use of gato:blabla also gives a match. But since the start is checked, xgato: does not match.

I suppose the original context looks something like this:

\^(?:(?:https?|ftp):)?\

That is, it has something to do with checking if a piece of text is a link (whether it starts with http: , https: or ftp: ). However, he still accepts the case that there is none of this because of the last ? .

    
14.08.2017 / 23:03
6

Your regex seems to look for a 'field' in the string in the default:

gato:
gatos:
cachorro:

(?:) means that your regex should match this pattern, but the catch does not go to the group or it is a group without capture.

At the end it means that the capture of that character or group is optional as in: ? can match gatos? or gato

Related:

Meaning of?:? =?! ? =?! in a regex

    
14.08.2017 / 22:49