【发布时间】:2014-06-20 10:51:37
【问题描述】:
我正在使用正则表达式来查找书页中是否存在字符串。下面是相同的代码。
String regex = ".*\\b.{0}" + searchText + ".{0}.*\\b";
Pattern pattern = Pattern.compile(regex);
pattern.matcher("This is the text area where I am trying to look for the particular text, which is in the variable searchText. This text will have the string (222M) as part of this string. The find method should give me a true result even if I don't enter the closing brakect of the word. This is a multiline string").find()
观察:
- 案例 1:当 searchText = "(222M)" 时
结果:找到字符串。
-
案例 2:当 searchText = "(222M" // 括号丢失时
我得到以下异常。
索引 22 附近的正则表达式模式中的括号嵌套不正确: .\b.{0}(1110r.{0}.\b
还有更好的选择在页面中查找字符串。使用 String.contains() 不一致。这是在安卓平台上。 ^
【问题讨论】:
-
请注意,您可以删除无用的
.{0},因为它匹配一个空字符串。
标签: java android regex string pattern-matching