Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Regular expression to remove string with brackets

  1. "10 10 [by]"
  2. "1232 1232 [en]"

expected:

  1. "10 10"
  2. "1232 1232"

I want to get string without [] and value inside it.
I tried companyCodeValue1.replaceFirst("\s*([^[]]*)$", "") but I have issue with []…
Does anyone know how to implement regular expression for catching that case?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

The regex you’re looking for it’s this one right here: \[.*\]

https://regex101.com/r/nDkL0H/1

Pattern p = Pattern.compile("\\[.*\\]");
Matcher m = p.matcher("10 10 [by]");
String res = m.replaceAll("");
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading