Java正则表达式和美元符号 [英] Java regular expressions and dollar sign

查看:399
本文介绍了Java正则表达式和美元符号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有Java字符串:

String b = "/feedback/com.school.edu.domain.feedback.Review$0/feedbackId");

我还生成了我想要匹配此字符串的模式:

I also have generated pattern against which I want to match this string:

String pattern = "/feedback/com.school.edu.domain.feedback.Review$0(.)*";

当我说 b.matches(pattern)它返回 false 。现在我知道美元符号是Java RegEx的一部分,但我不知道我的模式应该如何。我假设$ in pattern需要被一些转义字符替换,但不知道有多少。这个$符号对我很重要,因为它有助于我区分列表中的元素(美元后的数字),我不能没有它。

When I say b.matches(pattern) it returns false. Now I know dollar sign is part of Java RegEx, but I don't know how should my pattern look like. I am assuming that $ in pattern needs to be replaced by some escape characters, but don't know how many. This $ sign is important to me as it helps me distinguish elements in list (numbers after dollar), and I can't go without it.

推荐答案

您需要使用反斜杠( \ 中转义 $ c $ c>),但作为反斜杠是字符串中的转义字符,你需要转义反斜杠本身。

You need to escape $ in the regex with a back-slash (\), but as a back-slash is an escape character in strings you need to escape the back-slash itself.

您将需要以相同的方式转义任何特殊的正则表达式char,例如使用。。

You will need to escape any special regex char the same way, for example with ".".

String pattern = "/feedback/com\\.navteq\\.lcms\\.common\\.domain\\.poi\\.feedback\\.Review\\$0(.)*";

这篇关于Java正则表达式和美元符号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆