Special Regular Expression Analysis

I have a regex that looks something like this:

a(|bc)

This expression matches strings "a" perfectly, but does not match "abc". What does the expression in parentheses mean?

Edit: Using C # with the following code:

Match m = Regex.Match(TxtTest.Text, TxtRegex.Text);
if (m.Success)
  RtfErgebnis.Text = m.Value;
else
  RtfErgebnis.Text = "Gültig, aber kein Match!";

"TxTTest" contains the string to check (in this case, "abc"). "TxtRegex" contains the regular expression (in this case, "a (| bc)")

"RtfErgebnis" shows "Gültig, aber kein Match!" this means that the regular expression is valid, but the given test string does not match.

On a side note:

Expression

a(|bc)d

matches "ad" as well as "abcd". So why the previous expression does not match "abc"?

, . . , .

2:

"RtfErgebnis" "Gültig, aber kein Match!", , , .

, "a", .

+3
4

"". "a, bc". , "a" , "bc".

"a, bc, d". , , "d".

, "bc" "", :

a(bc)?

"a, bc", "abc", "a", .

+5

(|bc) , , - .

, :

a(bc|)

abc abc (bc), a ax ( ).

+3

Actually a (| bc) corresponds to abc

perl -n -e 'print "Output:$_" if /a(|bc)/; '
a
Output:a
abc
Output:abc
bc

Therefore, there is no contradictory behavior between a (| bc) and a (| bc) d

+1
source

Whether the symbol (| ab) returns a match "or" ab "for this group of matches depends on the ordering of your match and probably depends on the regular expression mechanism used. For example, in grep and sed, this matches only ab if the order is reversed (ab |):

echo abc | sed -n 's/a\(\bc\|\)/\1/p'

The above:

bc

And the following (| ab) returns nothing:

echo abc | sed -n 's/a\(\\|bc\)/\1/p'
+1
source

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


All Articles