简体   繁体   English

使用正则表达式提取特定模式内的字符串

[英]Using regex to extract string within a certain pattern

Lets say I have a string "lore epsum dimsum ${ITEM_NAME} wonton kimchi" .假设我有一个字符串"lore epsum dimsum ${ITEM_NAME} wonton kimchi"

I have created a regex that can extract ${ITEM_NAME} from anywhere in the string.我创建了一个正则表达式,可以从字符串中的任何位置提取${ITEM_NAME} That regex is, .{(.*.*)} .该正则表达式是.{(.*.*)}

How can I customize this regex to extract just the string between ${} which is ITEM_NAME ?如何自定义此正则表达式以仅提取${}之间的字符串ITEM_NAME

With {(.*.*)} pattern, you extract any substrings between the leftmost { and rightmost } .使用{(.*.*)}模式,您可以提取最左边{和最右边}之间的任何子字符串。

You need你需要

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

In Java:在 Java 中:

String regex = "\\$\\{([^{}]*)\\}";

See the regex demo .请参阅正则表达式演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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