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

'hasOwnProperty' in javascript -

How to put a lock and transaction on table using spring 4 or above using jdbcTemplate and annotations like @Transactional? -

How to understand 2 main() functions after using uftrace to profile the C++ program? -