带有后引用的Java String.replaceAll() [英] Java String.replaceAll() with back reference

查看:94
本文介绍了带有后引用的Java String.replaceAll()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一个Java Regex问题:
给定一个字符串,如果*位于字符串的开头或结尾处,请保留它,否则将其删除。
例如:

There is a Java Regex question: Given a string, if the "*" is at the start or the end of the string, keep it, otherwise, remove it. For example:


  1. * - > *

  2. ** - > **

  3. ******* - > **

  4. * abc ** def * - > * abcdef *

  1. * --> *
  2. ** --> **
  3. ******* --> **
  4. *abc**def* --> *abcdef*

答案是:

str.replaceAll("(^\\*)|(\\*$)|\\*", "$1$2");

我在我的机器上尝试了答案,但它确实有效。但我不知道它是如何工作的。

I tried the answer on my machine and it works. But I don't know how it works.

根据我的理解,所有匹配的子串应替换为 $ 1 $ 2 。但是,它的工作原理如下:

From my understanding, all matched substrings should be replaced with $1$2. However, it works as:


  1. (^ \\ *)已被替换 $ 1

  2. (\\ * $)替换为 $ 2

  3. \\ * 替换为空。

  1. (^\\*) replaced with $1,
  2. (\\*$) replaced with $2,
  3. \\* replaced with empty.

有人可以解释它是如何工作的吗?更具体地说,如果表达式之间存在 | ,那么 String.replaceAll()如何使用反向引用?

Could someone explain how it works? More specifically, if there is | between expressions, how String.replaceAll() works with back reference?

提前谢谢。

推荐答案

我会尝试解释发生了什么在正则表达式。

I'll try to explain what's happening in regex.

str.replaceAll("(^\\*)|(\\*$)|\\*", "$1$2");

$ 1 表示第一组(^ \\ *)
$ 2 代表第二组(\\\ \\ * $)

$1 represents first group which is (^\\*) $2 represents 2nd group (\\*$)

当你打电话给 str.replaceAll 时,你实际上是在捕捉两组以及其他所有内容,但在替换时,将捕获的文本替换为两组中捕获的内容。

when you call str.replaceAll, you are essentially capturing both groups and everything else but when replacing, replace captured text with whatever got captured in both groups.

示例: * abc ** def * - - > * abcdef *

找到正则表达式字符串,以 * 开头,它会放入 $ 1 group,接下来它将继续查找,直到在组末尾找到 * 并将其存储在<$ c $中C>#2 。现在更换它时将消除所有 * ,但存储在 $ 1 $ 2

Regex is found string starting with *, it will put in $1 group, next it will keep looking until it find * at end of group and store it in #2. now when replacing it will eliminate all * except one stored in $1 or $2

有关详细信息,请参阅捕获组

For more information see Capture Groups

这篇关于带有后引用的Java String.replaceAll()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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