What is the difference between "(ex1) | (ex2) | (ex3)" and "[(ex1) (ex2) (ex3)]"

I am trying to create some generic code to facilitate the use of regular expressions and think about how to implement the OR function.

The name is pretty accurate (ex1, ex2, ex3 - any regular expressions). Without considering the grouping, what is the difference between:

"(ex1)|(ex2)|(ex3)" 

and

 "[(ex1)(ex2)(ex3)]" 

Both of these should be or linked between named regular expressions, I just might miss something. Is any method more effective than the other?

+5
source share
7 answers
 (ex1)|(ex2)|(ex3) 

Here you write ex1 , ex2 and ex3 .

Here:

 [(ex1)(ex2)(ex3)] 

( i ) are quoted and processed as is, since they are enclosed in [ and ] ( character classes ), it corresponds to ( , ) , e , x , 1 , 2 and 3 .

Note that this is equivalent (order is not important):

 [ex123)(] 

Important notes for character sets:

The carriage (^) and hyphen (-) can be included as is. If you want to include a hyphen, you must put it at the very beginning of the character class. If you want to combine a caret as part of a character set, you should not put it as the first character:

  • [^]x] matches any that is not ] and x , where []^x] matches ] , ^ or x
  • [az] matches all letters from a to z , where [-az] matches - , a and z
+4
source

(ex1)|(ex2)|(ex3) matches ex1 (available in group 1), ex2 (available in group 2) or ex3 (available in group 3)

Regular expression visualization

Demo version of Debuggex


[(ex1)(ex2)(ex3)] matches ( , e , x , 1 , 2 , 3 or )

Regular expression visualization

Demo version of Debuggex

+5
source

They are fundamentally different.

(ex1)|(ex2)|(ex3) defines a sequence of alternating capture groups for the literal text ex1 , ex2 and ex3 . That is, either ex1 , if present, will be fixed in the first capture group; or ex2 , if any, will be recorded in the second capture group; or ex3 , if any, will be recorded in the third group. (This would be a rather odd expression, more likely would be (ex1|ex2|ex3) , which matches and captures either ex1 , ex2 , or ex3 .)

[(ex1)(ex2)(ex3)] defines a character class that will match any of the following characters (just one character): (ex1)23 . There are no capture groups, the text in [] processed literally.

The documentation for the Pattern class details how patterns work.

+1
source

In the first regular expression: (ex1)|(ex2)|(ex3) you will match the three groups indicated by the bracket (i.e. ex1 , ex2 , ex3 ), so you will get results that will match any ex1 regular expressions, any regular expressions ex2 and any regular expression ex3 .

While in the second: [(ex1)(ex2)(ex3)] there will be no groups (since you use brackets [] , and the brackets will be treated as characters. Thus, you will get everything that matches the expression (ex1)(ex2)(ex3) .

+1
source
  • In the first case, you have 3 groups (1 to 3) each with a sequence of characters separated by OR

  • In the second case, you have a character class containing the characters e , x , 1 , 2 , 3 , ( , ) and not a group

  • The first case will correspond to either ex1 or ex2 or ex3 and assign it to the corresponding group. Thus, when you enter "ex1" it matches and returns group 1 equal to "ex1" , group 2 and 3 null

  • Given the same input "ex1" in the second case, it will correspond to all characters, one at a time, in each subsequent match, and each character e , x and 1 will be assigned to group 0, i.e. whole coincidence

+1
source

first of all, in regex, [(abc)] means the correspondence of the character : a or b or c or ( or )

There is no "grouping" in the character class. (between [...] )

Another example of you is a group match, another.

0
source

"(ex1)|(ex2)|(ex3)"

If ex1 represents, then it must be captured by group 1, and if ex2 present, it will be captured by group 2, and if ex3 represents, it will be captured by group 3.

"[(ex1)(ex2)(ex3)]"

This corresponds to a single character from a given character class. It can be ( or e or x or 1 or 2 or 3 or )

0
source

Source: https://habr.com/ru/post/1207795/


All Articles