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

Popular posts from this blog

Command prompt result in label. Python 2.7 -

javascript - How do I use URL parameters to change link href on page? -

amazon web services - AWS Route53 Trying To Get Site To Resolve To www -