- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
所以我遇到了一个很好的边缘情况,如果替换字符串中有 $ 或\,则对字符串执行 replaceAll
会阻塞。
为什么替换字符串不只是替换为与我指定的正则表达式匹配的替换?我对这里的正则表达式有什么不理解的地方?
引自 Oracle Java 7 Documentation :
public String replaceAll(String replacement)
Replaces every subsequence of the input sequence that matches the pattern with the given replacement string.
This method first resets this matcher. It then scans the input sequence looking for matches of the pattern. Characters that are not part of any match are appended directly to the result string; each match is replaced in the result by the replacement string. The replacement string may contain references to captured subsequences as in the appendReplacement method.
Note that backslashes (\) and dollar signs ($) in the replacement string may cause the results to be different than if it were being treated as a literal replacement string. Dollar signs may be treated as references to captured subsequences as described above, and backslashes are used to escape literal characters in the replacement string.
最佳答案
美元符号是一个特殊字符;你必须逃避它:
"\\$"
请注意双反斜杠 - 这就是您在 java 中编写单个反斜杠的方式,这意味着 \$
被传递给 replace 方法。
美元符号表示对捕获组的反向引用:
"$1" // replace with group 1
"$2" // replace with group 2
还有一个特殊的第零组:
"$0" // replace with the entire match
关于java - Matcher.replaceAll - 在 $,\in replacement String 上阻塞,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16943048/
在 Java 正则表达式中使用尖括号时,“\\") 与 replaceAll (""),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/q
我有个小问题。我有一段文字,我必须在浏览器中阅读几次。每次,我打开这个文本,自动启动一个我写的 replaceAll。它非常简单、基本,但问题是当我下次替换时(每次我阅读这篇文章时)我有一个 repl
你好:D简短的问题:有什么区别 String geb = tf_datum.getText(); String sep = ""; //sep is short for seperator geb =
这个问题在这里已经有了答案: regex: How to escape backslashes and special characters? (1 个回答) 关闭 8 年前。 replaceAll
String.replaceAll() 和 Matcher.replaceAll()(在从 Regex.Pattern 创建的 Matcher 对象上)在性能方面是否存在已知差异? 另外,两者在高级
我需要从字符串中删除一些字符。目前我正在尝试删除“”(空格)和“-”(连字符)。我试过两种方法。他们都在运行 Android N Beta 的 Nexus 5X 上工作。它们都不适用于搭载 Andro
我在一些论坛上读到这样的神话,即通过执行类似的操作就足以通过 Veracode CWE 117(日志的不正确输出中和)问题。有人可以确认是否是这种情况吗? message.replaceAll("\
我想在不区分大小写的搜索中使用变量(而不是字符串)执行 JavaScript replaceAll(),同时保留匹配文本的大小写(在返回中)。例如, console.log('doc.p:', doc
这只是我尝试完成的事情的示例代码。 String s = "hello(1234aA)something"; String replaceString = "(1234aa)"; String s2
我在数据库的表中有一组原始文本,我需要使用一组单词替换此集合中的一些单词。 我将所有要替换的术语及其替代项放在一个文本文件中,如下所示 min=admin lelet=lambat lemot=lam
string 类中的方法 replaceAll(String regex, String replacement) 将此字符串中与给定正则表达式匹配的每个子字符串替换为给定的替换。是否可以否定正则表达
文档页面:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace
我想转换这个字符串 M,L,XL,XXL 到 size M,size L,size XL,size XXL 使用replaceAll()方法。可能吗? 最佳答案 您可以使用积极的前瞻来做到这一点。 S
两个问题: 已解决 将 $ 替换为另一个特殊字符 @ 并解决了问题。问题1 我有一个变量 String strVar ,其中包含以下文本(它从文件中读取)。现在我尝试使用 replaceAll() 替
考虑以下字符串: String s = "/static/201105-3805-somerandom/images/optional-folder/filename.gif"; 如何删除“stati
我需要一个正则表达式来执行以下替换: "\"Id\":\"123Abc\",\"foo\":\"bar\"" -> "\"Id\":null,\"foo\":\"bar\"" (change valu
我最近完成了以下编程练习:Acronym Generator 声明如下: In nearly every company each employee has a certain acronym con
所以这段代码按预期工作: String test = "ONE: this is a\ntest string."; System.out.println(test); test = test.rep
我有一个大的 json 存储在字符串中。我想更改字符串的某些部分,但收到此错误: Exception in thread "main" java.util.regex.PatternSyntaxExc
我编写了以下代码行: str.replaceAll("xxx(ayyy)xxx", "$1".substring(0,1).equals("a") ? "a" : "b"); 但是我发现“$1”.su
我是一名优秀的程序员,十分优秀!