How to replace different possible substrings (maybe even of different lengths) with a single substring?

huangapple go评论145阅读模式
英文:

How to replace different possible substrings (maybe even of different lengths) with a single substring?

问题

# Python
import re

input_string = "blahblahblahrgb(123, 214, 255)blahblahrgb(99, 0, 235)aefubaeofbiuaebfaef"
output_string = re.sub(r'rgb\(\d+,\s*\d+,\s*\d+\)', 'rgb(255, 255, 255)', input_string)
// JavaScript
let inputString = "blahblahblahrgb(123, 214, 255)blahblahrgb(99, 0, 235)aefubaeofbiuaebfaef";
let outputString = inputString.replace(/rgb\(\d+,\s*\d+,\s*\d+\)/g, 'rgb(255, 255, 255)');
// Java
import java.util.regex.*;

public class Main {
    public static void main(String[] args) {
        String inputString = "blahblahblahrgb(123, 214, 255)blahblahrgb(99, 0, 235)aefubaeofbiuaebfaef";
        String outputString = inputString.replaceAll("rgb\\(\\d+,\\s*\\d+,\\s*\\d+\\)", "rgb(255, 255, 255)");
    }
}
// Dart
void main() {
  String inputString = "blahblahblahrgb(123, 214, 255)blahblahrgb(99, 0, 235)aefubaeofbiuaebfaef";
  String outputString = inputString.replaceAllMapped(
      RegExp(r'rgb\(\d+,\s*\d+,\s*\d+\)'),
      (match) => 'rgb(255, 255, 255)'
  );
}

以上是在不同编程语言中实现替换的代码。

英文:

Suppose I have a huge string in following format -

"blahblahblahrgb(123, 214, 255)blahblahrgb(99, 0, 235)aefubaeofbiuaebfaef"

And I want to replace each instance of "rgb(x,x,x)" with "rgb(255,255,255)", with rest of the cahracters in string left intact. I can't use default string replace method as values inside rgb() can vary.

So the above string should become -

"blahblahblahrgb(255, 255, 255)blahblahrgb(255, 255, 255)aefubaeofbiuaebfaef"

Is there a default function to do that? or any efficient logic you can suggest?

Python, Javascript, Java, Dart or any language would do.

答案1

得分: 3

你的问题是正则表达式的一个完美用例。由于你要求使用多种语言,这里有一个 Python 正则表达式版本。

import re
rex = re.compile("rgb\(\d{1,3}, \d{1,3}, \d{1,3}\)")
myString = "blahblahblahrgb(123, 214, 255)blahblahrgb(99, 0, 235)aefubaeofbiuaebfaef"
newString = rex.sub("rgb(255, 255, 255)", mystring)

print(myString)
>>> blahblahblahrgb(123, 214, 255)blahblahrgb(99, 0, 235)aefubaeofbiuaebfaef

print(newString)
>>> blahblahblahrgb(255, 255, 255)blahblahrgb(255, 255, 255)aefubaeofbiuaebfaef

详细的正则表达式解释可以参考 Regex101

英文:

Your problem is a perfect usecase for Regular Expressions.
Since you asked for multiple languages, here is a python regex version.

import re
rex = re.compile("rgb\(\d{1,3}, \d{1,3}, \d{1,3}\)")
myString = "blahblahblahrgb(123, 214, 255)blahblahrgb(99, 0, 235)aefubaeofbiuaebfaef"
newString = rex.sub("rgb(255, 255, 255)", mystring)

print(myString)
>>> blahblahblahrgb(123, 214, 255)blahblahrgb(99, 0, 235)aefubaeofbiuaebfaef

print(newString)
>>> blahblahblahrgb(255, 255, 255)blahblahrgb(255, 255, 255)aefubaeofbiuaebfaef

See Regex101 for a step by step explanation of the regex.

答案2

得分: 2

这是一个在Java中的快速正则表达式解决方案

public String replaceAllRGBToWhite(String str) {
    return str.replaceAll("rgb\\\\(\\\\d{1,3}, \\\\d{1,3}, \\\\d{1,3}\\\\)", "rgb(255, 255, 255)");
}
英文:

Here is a quick regex solution in Java

public String replaceAllRGBToWhite(String str) {
    return str.replaceAll("rgb\\(\\d{1,3}, \\d{1,3}, \\d{1,3}\\)", "rgb(255, 255, 255)");
}

huangapple
  • 本文由 发表于 2020年9月9日 14:11:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/63805688.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定