Java regex删除任何东西的单引号

时间:2022-09-15 16:14:46

I have a huge string that looks like:

我有一根巨大的绳子

widgets: '{some-really-huge-string-omitted-for-brevity}'

And I would like to remove the single-quotes so that I get:

我想去掉单引号,这样我就得到:

widgets: {some-really-huge-string-omitted-for-brevity}

In reality, some-really-huge-string-omitted-for-brevity is a massive string containing alphanumeric characters, punctuation, basically everything under the sun. My best attempt so far:

在现实中,有一种叫做“简写为简”的大字符串包含了字母数字字符,标点符号,基本上都是在太阳底下。到目前为止我最好的尝试:

bigString = bigString.replaceAll("widgets: '\\{*\\}'", "widgets: \\{*\\}");

Doesn't throw any exceptions/errors, but also doesn't change a thing! When I print bigString, it's still the same as before the replace! Any ideas? Thanks in advance.

不会抛出任何异常/错误,但也不会改变任何事情!当我打印bigString时,它仍然和在替换之前一样!什么好主意吗?提前谢谢。

3 个解决方案

#1


1  

If the quotes are always in those places. (ie. 9th and last characters of the String), then just use substring to trim and rejoin. Scanning through the entire String would be slow and pointless.

如果引号总是在这些地方。(即。然后使用子字符串来修饰和重新连接。扫描整个字符串将是缓慢和无意义的。

String trimmed = hugeString.substring(0, 9) + hugeString.substring(10, hugeString.length() - 1);

Updated

更新

Seeing as you accepted this answer, this might be a more efficient version:

既然你接受了这个答案,这可能是一个更有效的版本:

StringBuilder b = new StringBuilder(hugeString);
b.deleteCharAt(9);
b.deleteCharAt(b.length() - 1);
String trimmed = b.toString();

#2


1  

String str = "widgets: '{some-really-huge-string-omitted-for-brevity}'";
System.out.println (str.replaceAll ("'([^']*)'", "$1"));

#3


1  

string= string.replace("'", "");

If you want to remove all single quotes around anything try above code .

如果您想删除所有的单引号,请尝试以上代码。

string= string.replace("'{", "{").replace("}'","}");

If you want to remove single quotes before opening curly braces "{" and closing curly braces "}".

如果您想在打开大括号“{”和结束大括号“}”之前删除单引号。

#1


1  

If the quotes are always in those places. (ie. 9th and last characters of the String), then just use substring to trim and rejoin. Scanning through the entire String would be slow and pointless.

如果引号总是在这些地方。(即。然后使用子字符串来修饰和重新连接。扫描整个字符串将是缓慢和无意义的。

String trimmed = hugeString.substring(0, 9) + hugeString.substring(10, hugeString.length() - 1);

Updated

更新

Seeing as you accepted this answer, this might be a more efficient version:

既然你接受了这个答案,这可能是一个更有效的版本:

StringBuilder b = new StringBuilder(hugeString);
b.deleteCharAt(9);
b.deleteCharAt(b.length() - 1);
String trimmed = b.toString();

#2


1  

String str = "widgets: '{some-really-huge-string-omitted-for-brevity}'";
System.out.println (str.replaceAll ("'([^']*)'", "$1"));

#3


1  

string= string.replace("'", "");

If you want to remove all single quotes around anything try above code .

如果您想删除所有的单引号,请尝试以上代码。

string= string.replace("'{", "{").replace("}'","}");

If you want to remove single quotes before opening curly braces "{" and closing curly braces "}".

如果您想在打开大括号“{”和结束大括号“}”之前删除单引号。