简体   繁体   中英

Regex to get text between two characters in Java

I've this string:

Test 123 ${tag1} lorem ipsum ${tag2} ${tag3; tag4} ${tag5; tag6} dixit lorem ${tag7} dixit lorem ${tag8}.

I would like to have a regex to select only the text between ${tag; tag} .

I tried with this:

\$\{(.*?)(; )(.*?)}

but can't get what I'm expecting. Any suggest? Thanks

If the inner text inside the ${...;...} cannot contain { , } and ; use a negated character class solution:

\$\{([^;{}]*);\s*([^{};]*)}

See the regex demo .

If the plan is to match word chars only you may simplify it to

\$\{(\w+)\s*;\s*(\w+)}

See another regex demo .

Details

  • \\$\\{ - a ${ substring
  • ([^;{}]*) - Group 1: any 0+ chars other than ; , { and } ( (\\w+) would match 1 or more word chars, letters, digits, underscores)
  • \\s*;\\s* - a ; enclosed with 0+ whitespaces
  • ([^;{}]*) - Group 2: any 0+ chars other than ; , { and }
  • } - a } char.

Java demo :

String s = "Test 123 ${tag1} lorem ipsum ${tag2} ${tag3; tag4} ${tag5; tag6} dixit lorem ${tag7} dixit lorem ${tag8}.";
Pattern pattern = Pattern.compile("\\$\\{(\\w+)\\s*;\\s*(\\w+)}");
Matcher matcher = pattern.matcher(s);
while (matcher.find()){
    System.out.println(matcher.group());
    System.out.println(matcher.group(1));
    System.out.println(matcher.group(2)); 
    System.out.println("=== END OF MATCH ===");
} 

Output:

${tag3; tag4}
tag3
tag4
=== END OF MATCH ===
${tag5; tag6}
tag5
tag6
=== END OF MATCH ===

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM