1

I am a bit new to Regular Expressions. I am trying to create a Regular Expression in Java with a variable inside.

I would like to match "var", "(var)", "[var]", or "{var}" and any case variation of the variable var.

I have tried this, and it seems to have an exception:
java.util.regex.PatternSyntaxException: Unclosed character class near index 20

Does anyone have any suggestions how to fix and improve this?

String s = "[Tom] The rest of the title";
String v = "Tom";

s = s.replaceAll("(?i)[({/[]*" + v + "[)}\]]*", "");
System.out.println(s);
2
  • Should [Tom) match in your example? Commented Jun 16, 2016 at 0:36
  • @AndrewRueckert Thanks for the response, no it should not. Commented Jun 16, 2016 at 0:38

2 Answers 2

1

Try:

String s = "[Tom] The rest of the title";
String v = "Tom";

String myRegexp = String.format("(?i)\\[%s\\]|\\{%s\\}|\\(%s\\)|%s", v,v,v,v);
System.out.println(myRegexp);

s = s.replaceAll(myRegexp, "");
System.out.println(s);

This line:

String myRegexp = String.format("(?i)\\[%s\\]|\\{%s\\}|\\(%s\\)|%s", v,v,v,v);

builds a simple regular expression: (?i)\[Tom\]|\{Tom\}|\(Tom\)|Tom

All braces: {}[]() are special characters in regular expressions, and they have to be escaped by \ character (in Java string it must be \\).

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for your response! Does this work for "too cool for school" to "[Too Cool for School] Hello"? It doesn't seem to remove this from the title!
Yes it works if you replace Tom with a new string and assign it to v ==> String v = "Too Cool for School";
great answer! This is exactly what I was looking for, but extending it a bit more, what about removing all special characters around "Tom"? For example, the string could be something like "{[[Tom]]}" and I would want to replace the entire string with an empty string! Is it possible to remove all characters until an empty space on both sides?
How would you match any number of special characters on both sides?
1

Matching pairs of braces ( to ), { to }, etc. is going to be messy with regular expressions, since the relationship isn't built into the language. A naieve solution would be:

String s = "[Tom] The rest of the title (TOM] tom";
String v = "Tom";
Pattern p = Pattern.compile("\\(" + v + "\\)|\\{" + v + "\\}|\\[" + v + "\\]|(?![{(\\[])" + v + "(?![})\\]])", Pattern.CASE_INSENSITIVE);
Matcher m = p.matcher(s);
System.out.println('\'' + m.replaceAll("") + '\'');
// ' The rest of the title (TOM] '

The head-ache-y part of regular expressions in Java is that you need to double escape any literal characters with \\, since you want a literal backslash character to appear in the string that you're passing to the regex compiler.

1 Comment

Is there anything that's more readable / elegant that works for most of the cases?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.