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...

问题:

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


回答1:

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 ===
  • 发表于 2019-02-21 06:24
  • 阅读 ( 179 )
  • 分类:sof

条评论

请先 登录 后评论
不写代码的码农
小编

篇文章

作家榜 »

  1. 小编 文章
返回顶部
部分文章转自于网络,若有侵权请联系我们删除