java - What's the difference between "(ex1)|(ex2)|(ex3)" and "[(ex1)(ex2)(ex3)]" -
i'm trying create general code ease usage of regexes, , thinking how implement or function.
the title pretty accurate (ex1,ex2,ex3 regular expressions). not considering grouping, what's difference between:
"(ex1)|(ex2)|(ex3)"
and
"[(ex1)(ex2)(ex3)]"
these both should or relation between named regexes, might missing something. way 1 more efficient other?
(ex1)|(ex2)|(ex3)
here capturing ex1
, ex2
, ex3
.
here:
[(ex1)(ex2)(ex3)]
(
, )
quoted , treated since they're enclosed in [
, ]
(character classes), matches (
, )
, e
, x
, 1
, 2
, 3
.
note it's equivalent (the order not important):
[ex123)(]
important notes on character sets:
the caret (^) , hyphen (-) can included is. if want include hyphen, should place in beginning of character class. if want match caret part of character set, should not put first character:
[^]x]
matches that's not]
,x
[]^x]
matches]
,^
orx
[a-z]
matches lettersa
z
[-az]
matches-
,a
,z
Comments
Post a Comment