简体   繁体   English

java中的正则表达式从给定的字符串中找到类似$ {...}的模式

[英]Regex in java to find pattern like ${…} from given string

I am a noob to regex. 我是正则表达式的菜鸟。

I have string like:- 我有这样的字符串: -

String str = "sbs 01.00 sip ${dreamworks.values} print ${fwVer} to 
              used ${lang} en given ${model}  in ${region}";

and i have to extract all patterns matched with this type ${....} 我必须提取与此类型匹配的所有模式$ {....}

Like:- for given str result should be 喜欢: - 对于给定的str结果应该是

${dreamworks.values} 
${fwVer}   
${lang}
${model}
${region}

further if it finds any duplicates then gives only one . 如果它发现任何重复,那么只给出一个。 for ex:- 对于前: -

String feed = "sip ${dreamworks.values} print ${fwVer} to ${fwVer} used
                ${lang} en ${lang}given ${model}  in ${region}"

result should be:- 结果应该是: -

 ${dreamworks.values}  
 ${fwVer}   
 ${lang}
 ${model}
 ${region}  

only 只要

this is my answer:- 这是我的答案: -

PLACEHOLDER_PATTERN = "\\$\\{\\w+\\}";

but this one not giving the correct result. 但是这个没有给出正确的结果。 it gives only 它只给出

${fwVer}
${lang}
${model}
${region}

So please suggest me correct regex. 所以请建议我正确的正则表达式。

You are not considering the . 你不是在考虑. in between the word. 在这个词之间。 \\\\w does not include the dot(.) . \\\\w不包括dot(.)

You need to change your pattern to: - 您需要将模式更改为: -

PLACEHOLDER_PATTERN = "\\$\\{.+?\\}";

dot(.) matches everything, and that is what you want right? dot(.)匹配一切,这就是你想要的吗?

Also, I have used here reluctant quantifier - .+? 另外,我在这里使用了reluctant量词 - .+? so that it only matches the first } after { , since if you use a greedy quantifier (.+) , dot(.) will also match the } in the way till it finds the last } . 所以它只匹配}之后} { ,因为如果你使用贪婪量词(.+)dot(.)也会匹配} ,直到它找到最后一个}


UPDATE: - 更新: -

To get just the unique values, you can use this pattern: - 要获得唯一值,您可以使用此模式: -

"(\\$\\{[^}]+\\})(?!.*?\\1)"

It will match only those pattern, which is not followed by the string containing the same pattern. 它将仅匹配那些不包含相同模式的字符串的模式。

NOTE: - Here, I have used [^}] , in place of .+? 注意: -这里,我用[^}]代替.+? . It will match any character except } . 它将匹配除}之外的任何字符。 So, now in this case, you don't need a reluctant quantifier. 所以,现在在这种情况下,你不需要一个reluctant量词。

\\1 is used for backreferencing , but we need to escape it with a backslash, and hence \\\\1 , and (?!...) is used for negative look ahead . \\1用于backreferencing ,但我们需要用反斜杠转义它,因此\\\\1(?!...)用于negative look ahead

Thats is, because the . 多数民众赞成是因为. is not included in \\w . 未列入\\w You need to create your own character class then and add it there. 您需要创建自己的角色类,然后将其添加到那里。

PLACEHOLDER_PATTERN = "\\$\\{[\\w.]+\\}";

See the pattern here on Regexr . 请参阅Regexr上的模式。

However, this does not solve the problem, that you want no duplicates, but that is not a job for regular expressions. 但是,这并没有解决问题,您不需要重复,但这不是正则表达式的工作。

If there could be more different characters between the curly brackets, then Rohits answer is better, that would match any characters till the closing bracket. 如果大括号之间可能有更多不同的字符,那么Rohits的答案会更好,这将匹配任何字符直到结束括号。

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

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