简体   繁体   English

Java Regex删除字符串之间的文本,并在字符串中包括括号

[英]Java Regex Remove Text Between and Including Parenthesis from String

I am programming in Java, and I have a few Strings that look similar to this: 我正在用Java进行编程,并且有一些看起来与此类似的字符串:

"Avg. Price ($/lb)" 
"Average Price ($/kg)"

I want to remove the ($/lb) and ($/kg) from both Strings and be left with 我想从两个字符串中删除($/lb)($/kg)并留下

"Avg. Price" 
"Average Price".

My code checks whether a String str variable matches one of the strings above, and if it does, replaces the text inside including the parentheses with an empty string: 我的代码检查String str变量是否与上面的字符串之一匹配,如果匹配,则用空字符串替换其中的文本(包括括号):

    if(str.matches(".*\\(.+?\\)")){

           str = str.replaceFirst("\\(.+?\\)", "");
    }

When I change str.matches to str.contains("$/lb"); 当我将str.matches更改为str.contains(“ $ / lb”); as a test, the wanted substring is removed which leads me to believe there is something wrong with the if statement. 作为测试,删除了所需的子字符串,这使我相信if语句有问题。 Any help as to what I am doing wrong? 对我做错了什么有帮助吗? Thank you. 谢谢。

Update I changed the if statement to: 更新我将if语句更改为:

if(str.contains("(") && str.contains (")"))

Maybe not an elegant solution but it seems to work. 也许这不是一个优雅的解决方案,但似乎可行。

str.matches has always been problematic for me. str.matches一直是我的难题。 I think it implies a '^' and '$' surrounding the regex you pass it. 我认为这意味着您通过它的正则表达式周围有一个“ ^”和“ $”。

Since you just care about replacing any occurrence of the string in question - try the following: 由于您只关心替换问题字符串的任何出现,请尝试以下操作:

str = str.replaceAll("\\s+\\(\\$\\/(lb|kg)\\)", "");

There is an online regex testing tool that you can also try out to see how your expression works out. 有一个在线正则表达式测试工具 ,您也可以尝试查看表达式的工作方式。

EDIT With regard to your comment, the expression could be altered to just: 编辑关于您的评论,可以将表达式更改为:

str = str.replaceAll("\\s+\\([^)]+\\)$", "");

This would mean, find any section of content starting with one or more white-space characters, followed by a literal '(', then look for any sequence of non-')' characters, followed by a literal ')' at the end of the line. 这意味着,找到内容的任何部分,以一个或多个空格字符开头,后跟文字“(”,然后查找任何非“)”字符序列,最后寻找文字“)”的线。

Is that more in-line with your expectation? 这更符合您的期望吗?

Additionally, heed the comment with regard to 'matches()' vs 'find()' that is very much so what is impacting operation here for you. 此外,请注意有关“ matches()”与“ find()”的评论,因此对您的操作有何影响。

Unlike most other popular application languages, the matches() method in java only returns true if the regex matches the whole string (not part of the string like in perl, ruby, php, javascript etc). 与大多数其他流行的应用程序语言不同,java中的matches()方法仅在正则表达式匹配整个字符串(而不是像perl,ruby,php,javascript等字符串的一部分 matches()时才返回true

The regex to match bracketed input, including any leading spaces, is: 匹配方括号输入(包括任何前导空格)的正则表达式为:

" *\\(.*?\\)"

and the code to use this to remove matches is: 和用于删除匹配项的代码是:

str = str.replaceAll(" *\\(.+?\\)", "");

Here's some test code: 这是一些测试代码:

String str = "foo (stuff) bar(whatever)";
str = str.replaceAll(" *\\(.+?\\)", "");
System.out.println(str);

Output: 输出:

"foo bar"

This code is working fine. 这段代码工作正常。

    String str = "Avg. Price ($/lb) Average Price ($/kg)";

    if (str.matches(".*\\(.+?\\)")) {
        str = str.replaceFirst("\\(.+?\\)", "");
    }
    System.out.println("str: "+str);

This will print Avg. 这将打印平均。 Price Average Price which is what you need. 平均价格这是您需要的价格

Note: I changed replaceFirst with replaceAll here. 注意:我在这里将replaceFirst替换replaceAll

String first = "^(\\\\w+\\\\.\\\\s\\\\w+)"; 字符串优先=“ ^(\\\\ w + \\\\。\\\\ s \\\\ w +)”;

This would print out Avg. 这将打印出平均 Price 价钱

String second="(\\\\w\\\\s\\\\w)"; 字符串second =“(\\\\ w \\\\ s \\\\ w)”;

This would print out Average Price 这将打印出平均价格

hope this simple answer helps 希望这个简单的答案有帮助

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

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