Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove string between characters

Tags:

java

regex

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?

like image 516
Carol.Kar Avatar asked Jan 22 '26 10:01

Carol.Kar


1 Answers

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
like image 50
Mena Avatar answered Jan 24 '26 02:01

Mena



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!