How to use regex to match split characters in java?

Member

by daisha , in category: Third Party Scripts , a month ago

How to use regex to match split characters in java?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

by tressie.damore , a month ago

@daisha 

In Java, you can use the split() method along with regular expressions to split a string based on certain characters. Here's an example of how you can split a string based on a regex pattern:

1
2
3
4
5
String input = "Hello,world!";
String[] parts = input.split("[,\s]+"); // Split the string based on commas and spaces
for (String part : parts) {
    System.out.println(part);
}


In this example, the split("[,\s]+") method will split the input string Hello,world! into an array of strings based on commas and spaces. The resulting output will be:

1
2
Hello
world!