How to Split Strings Using Regular Expressions in Java? Last Updated : 31 Jan, 2024 Comments Improve Suggest changes Like Article Like Report Split a String while passing a regular expression (Regex) in the argument and a single String will split based on (Regex), as a result, we can store the string on the Array of strings. In this article, we will learn how to split the string based on the given regular expression. Example of Split String Using Regular ExpressionInput: String s1 = "hello-from-GeeksforGeeks" String regex = "-" Output: "hello from GeeksforGeeks" Java Program to Split the String based on the Regular ExpressionFirst, we apply a String.split() on the given String and pass the given regex in the argument. It will return an Array of strings and we store it in the Array then we print the Array. Syntaxstr.split(regular_expression)Below is the implementation of the topic: Java // java program to split a string. import java.io.*; class GFG { public static void main(String[] args) { // give String String s1 = "hello-from-GeeksforGeeks"; // given regex String regex = "-"; // apply split() method on the given string and pass // regex in the argument. String[] split = s1.split(regex); // return an array of strings // printing output for (String s : split) { System.out.print(s + " "); } } } Outputhello from GeeksforGeeks Explaination of the above Program:Pass any kind of Regex it would be a single character or String to the split() method that will return the string array with replacing the elements.But if you try to split String with some special characters like * or / . which are predefined characters in regular expression.So it will not work so you need to pass regex in this form [//*] Here you need to use backslash(//) and the square bracket represents the specific character. Comment More infoAdvertise with us Next Article How to Split Strings Using Regular Expressions in Java? devanshuma3v23 Follow Improve Article Tags : Java Java Examples Practice Tags : Java Similar Reads How to validate a Username using Regular Expressions in Java Given a string str which represents a username, the task is to validate this username with the help of Regular Expressions. A username is considered valid if all the following constraints are satisfied: The username consists of 6 to 30 characters inclusive. If the username consists of less than 6 or 3 min read Convert user input string into regular expression using JavaScript In this article, we will convert the user input string into a regular expression using JavaScript.To convert user input into a regular expression in JavaScript, you can use the RegExp constructor. The RegExp constructor takes a string as its argument and converts it into a regular expression object 2 min read Regular Expressions in Java In Java, Regular Expressions or Regex (in short) in Java is an API for defining String patterns that can be used for searching, manipulating, and editing a string in Java. Email validation and passwords are a few areas of strings where Regex is widely used to define the constraints. Regular Expressi 7 min read How to Use Regular Expression as a Substitute of endsWith() Method in Java? So primarily discuss what is endsWith() method is, so it is a method of String class that checks whether the string ends with a specified suffix. This method returns a boolean value true or false. Syntax: public boolean endsWith(String suff) Parameter: specified suffix part Return: Boolean value, he 3 min read How to split a string in C/C++, Python and Java? Splitting a string by some delimiter is a very common task. For example, we have a comma-separated list of items from a file and we want individual items in an array. Almost all programming languages, provide a function split a string by some delimiter. In C: // Splits str[] according to given delim 7 min read Like