简体   繁体   中英

Remove string between characters

I would like to remove everything(for the chars like {}$* \\w+ "" ) which is between ; and # :

For example I would like to remove from this string:

Input:

OR(AND(CA18*CB18);M10#;ABZZ/kld // remove ;M10#

Output:

OR(AND(CA18*CB18);ABZZ/kld

I tried it with this regular expression:

^[;]\\w+([A-Za-z0-9])[#]

However, this does not seem to work any recommendations?

Try this solution:

String input = "OR(AND(CA18*CB18);M10#;ABZZ/kld"; // remove ;M10#
// using String.replaceAll here instead of Pattern/Matcher
//
//                                   | starts with ; included
//                                   || any character, reluctantly quantified
//                                   ||  | ends with # included
//                                   ||  |   | replace all instances with empty
//                                   ||  |   | string
System.out.println(input.replaceAll(";.+?#", ""));

Output

OR(AND(CA18*CB18);ABZZ/kld
  • ^ means "start of the string", ie your string must start with ; , which is not the case (it starts with O ).

  • \\w+([A-Za-z0-9]) is quite redundant: \\w is actually [A-Za-z0-9_] , so unless you really need this _ distinction, \\w+ should be enough

So, simply try with: ;\\w+#


If you need any char between ; and # (ie not only [A-Za-z0-9_] ): ;[^;#]+#

正则表达式可视化

Debuggex Demo

Your regex only accepts two non symbol characters

;[A-z0-9]*?#

Will grab anything in between. The same regex with a + instead of the * will only match instances with at least on char in between the symbols.

solution for your updated question would be:

// the actual regex is ;[{}$*\w"']+?#, but extra escaping is needed for Java:
input.replaceAll(";[{}$*\\w\"']+?#", "");

where you can update the character set between [] to match your actual requirements as you discover more edge cases..

if you decided you need a black-list of characters instead, you could use expression with negated character set ( ^ inside [], do not confuse with ^ at the start of a regex, which denotes the beginning of a string):

;[^;#]+?#

This should do the work:

 String sURL = "OR(AND(CA18*CB18);M10#;ABZZ/kld";
 System.out.println(sURL.replaceAll(";\\w+?#", ""));

试试这个正则表达式

[^;]*?#;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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