Carol.Kar
Carol.Kar

Reputation: 5215

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?

Upvotes: 3

Views: 182

Answers (6)

Aprillion
Aprillion

Reputation: 22340

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):

;[^;#]+?#

Upvotes: 2

vikrant
vikrant

Reputation: 397

Try this regular expression

[^;]*?#;

Upvotes: 0

sp00m
sp00m

Reputation: 48837

  • ^ means "start of the string", i.e. 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 # (i.e. not only [A-Za-z0-9_]): ;[^;#]+#

Regular expression visualization

Debuggex Demo

Upvotes: 4

Jens
Jens

Reputation: 69515

This should do the work:

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

Upvotes: 1

Mena
Mena

Reputation: 48444

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

Upvotes: 4

Adam Yost
Adam Yost

Reputation: 3625

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.

Upvotes: 2

Related Questions