java - Find between custom tag if between tag has text or not -
in below code can find text between <p|a|s>hello there</>
such p|a|s
, hello there
without problem
pattern mfta_regex = pattern.compile("<(.+?)>(.+?)</>"); matcher matcher = mfta_regex.matcher("<p|a|s>hello there</>"); if (matcher.find()) { log.e("tag ",matcher.group(1)); log.e("text ",matcher.group(2)); }
now when don't have p|a|s
<>hello there</>
matcher couldn't find. in string p|a|s
optional, how can change pattern.compile("<(.+?)>(.+?)</>");
resolve problem?
optional ?
on group should outside group, i.e. after )
:
<(.+)?>
or if want match empty string, then:
<(.*)>
see working example on regex101.com
Comments
Post a Comment