Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ignoring text between parenthesis in Regex

Tags:

java

regex

My goal is to read in the String and ignore the text in parenthesis.

public static void main(String[] args) {
    Pattern checkRegex= Pattern.compile("([a-zA-Z]{3,30}\\s*){2}");
    
    Matcher regexMatcher=checkRegex.matcher("James Hunt(Skateboarder)");
    
    while(regexMatcher.find()){
    System.out.println(regexMatcher.group().trim());
}

The current output is:

James Hunt

Skateboarder

Essentially what I want is for the output to be only "James Hunt". What is a suitable Regex pattern to use in this type of situation?

like image 576
PedroPovedaQ Avatar asked Dec 12 '25 08:12

PedroPovedaQ


1 Answers

This will work for all non-nested parentheses in your given String:

String input = "James Hunt(Skateboarder)";
String output = input.replaceAll("\\([^)]*?\\)", "");
like image 66
jlordo Avatar answered Dec 14 '25 03:12

jlordo



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!