简体   繁体   English

用匹配的正则表达式的一部分替换字符串

[英]Replace string with part of the matching regex

I have a long string.我有一根长绳子。 I want to replace all the matches with part of the matching regex (group).我想用匹配的正则表达式(组)的一部分替换所有匹配项。

For example:例如:

String = "This is a great day, is it not? If there is something, THIS IS it. <b>is</b>".

I want to replace all the words "is" by, let's say, "<h1>is</h1>" .我想用"<h1>is</h1>"替换所有单词"is" ”。 The case should remain the same as original.外壳应该与原来的一样。 So the final string I want is:所以我想要的最后一个字符串是:

This <h1>is</h1> a great day, <h1>is</h1> it not? If there <h1>is</h1> something, 
THIS <h1>IS</h1> it. <b><h1>is</h1></b>.

The regex I was trying:我正在尝试的正则表达式:

Pattern pattern = Pattern.compile("[.>, ](is)[.<, ]", Pattern.CASE_INSENSITIVE);

The Matcher class is commonly used in conjunction with Pattern . Matcher类通常与Pattern一起使用。 Use the Matcher.replaceAll() method to replace all matches in the string 使用Matcher.replaceAll()方法替换字符串中的所有匹配项

String str = "This is a great day...";
Pattern p = Pattern.compile("\\bis\\b", Pattern.CASE_INSENSITIVE);
Matcher m = p.matcher(str);
String result = m.replaceAll("<h1>is</h1>");

Note: Using the \\b regex command will match on a word boundary (like whitespace). 注意:使用\\b regex命令将匹配单词边界(如空格)。 This is helpful to use in order to ensure that only the word "is" is matched and not words that contain the letters "i" and "s" (like "island"). 这有助于确保仅匹配单词“is”而不包含字母“i”和“s”(如“island”)的单词。

Like this: 像这样:

str = str.replaceAll(yourRegex, "<h1>$1</h1>");

The $1 refers to the text captured by group #1 in your regex. $1指的是正则表达式中#1组捕获的文本。

Michael's answer is better, but if you happen to specifically only want [.>, ] and [.<, ] as boundaries, you can do it like this: 迈克尔的答案更好,但如果你碰巧只想要[.>, ][.<, ]作为边界,你可以这样做:

String input = "This is a great day, is it not? If there is something, THIS IS it. <b>is</b>";
Pattern p = Pattern.compile("(?<=[.>, ])(is)(?=[.<, ])", Pattern.CASE_INSENSITIVE);
Matcher m = p.matcher(input);
String result = m.replaceAll("<h1>$1</h1>");
yourStr.replaceAll("(?i)([.>, ])(is)([.<, ])","$1<h1>$2</h1>$3")

(?i) to indicate ignoring case; (?i)表示无视案件; wrap everything your want to reuse with brackets, reuse them with $1 $2 and $3, concatenate them into what you want. 用括号包装你想要重复使用的所有东西,用$ 1 $ 2和$ 3重复使用它们,将它们连接成你想要的东西。

Simply use a backreference for that. 只需使用反向引用即可。

"This is a great day, is it not? If there is something, THIS IS it. <b>is</b>".replaceAll("[.>, ](is)[.<, ]", "<h1>$2</h1>"); should do. 应该做。

It may be a late addition, but if anyone is looking for this like 这可能是一个后期添加,但如果有人正在寻找这样的
Searching for 'thing' and also he needs 'Something' too to be taken as result, 寻找'东西'并且他也需要“东西”才能被视为结果,

Pattern p = Pattern.compile("([^ ] )is([^ \\.] )"); 模式p = Pattern.compile(“([^] )是([^ \\。] )”);
String result = m.replaceAll("<\\h1>$1is$2</h1>"); 字符串结果= m.replaceAll(“<\\ h1> $ 1is $ 2 </ h1>”);

will result <\\h1>Something</h1> too 将导致<\\ h1> Something </ h1>

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

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