Found 7442 Articles for Java

Splitting the text using java.util.regex package

Maruthi Krishna
Updated on 13-Jan-2020 06:40:53

149 Views

The split() method of the String class accepts a regular expression, splits the current input text into tokens and returns them as a string array.Example Live Demoimport java.util.Scanner; public class Example{    public static void main(String[] args) {       Scanner sc = new Scanner(System.in);       System.out.println("Enter input text: ");       String input = sc.nextLine();       String[] strArray = input.split("\d");       for (int i=0; i

Named captured groups Java regular expressions

Maruthi Krishna
Updated on 13-Jan-2020 06:36:49

3K+ Views

Named capturing groups allows you to reference the groups by names. Java started supporting captured groups since SE7.Example Live Demoimport java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; public class ReplaceAll{    public static void main(String[] args) {       Scanner sc = new Scanner(System.in);       System.out.println("Enter input text: ");       String input = sc.nextLine();       String regex = "(?[\d]{2})-(?[\d]{5})-(?[\d]{6})";       //Creating a pattern object       Pattern pattern = Pattern.compile(regex);       //Matching the compiled pattern in the String       Matcher matcher = pattern.matcher(input);       while (matcher.find()) { ... Read More

Replacing all the matched contents Java regular expressions

Maruthi Krishna
Updated on 13-Jan-2020 06:34:25

1K+ Views

Once you compile the required regular expression and retrieved the matcher object by passing the input string as a parameter to the matcher() method.You can replace all the matched parts of the input string with another str4ing using the replaceAll() method of the Matcher class.This method accepts a string (replacement string) and replaces all the matches in the input string with it and returns the result.Example 1 Live Demoimport java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; public class ReplaceAll{    public static void main(String[] args) {       Scanner sc = new Scanner(System.in);       System.out.println("Enter input text: ");     ... Read More

Finding a Match Within Another Match Java regular expressions

Maruthi Krishna
Updated on 13-Jan-2020 06:30:59

306 Views

To match a pattern within another match you need to compile the regular expression to match the outer pattern find the match retrieve the results and pass the results as input to the inner Matcher object.Example Live Demoimport java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; public class MatcherExample {    public static void main(String[] args) {       int start = 0, len = -1;       Scanner sc = new Scanner(System.in);       System.out.println("Enter input text: ");       String input = sc.nextLine();       String regexOuter = "(.*?)";       String regexInner = "\d+";   ... Read More

Getting the list of all the matches Java regular expressions

Maruthi Krishna
Updated on 13-Jan-2020 06:27:52

11K+ Views

Java does not provide any method to retrieve the list of all matches we need to use Lists and add the results to it in the while loop.Example Live Demoimport java.util.ArrayList; import java.util.Iterator; import java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; public class ListOfMatches{    public static void main(String[] args) {       Scanner sc = new Scanner(System.in);       System.out.println("Enter input text: ");       String input = sc.nextLine();       String regex = "\d+";       //Creating a pattern object       Pattern pattern = Pattern.compile(regex);       ArrayList list = new ArrayList();   ... Read More

Determining the position and length of the match Java regex

Maruthi Krishna
Updated on 13-Jan-2020 06:22:03

1K+ Views

The start() method of the java.util.regex.Matcher class returns the starting position of the match (if a match occurred).Similarly, the end() method of the Matcher class returns the ending position of the match.Therefore, return value of the start() method will be the starting position of the match and the difference between the return values of the end() and start() methods will be the length of the match.Example Live Demoimport java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; public class MatcherExample {    public static void main(String[] args) {       int start = 0, len = -1;       Scanner sc = new ... Read More

Non capturing groups Java regular expressions:

Maruthi Krishna
Updated on 21-Feb-2020 11:31:41

1K+ Views

Using capturing groups you can treat multiple characters as a single unit. You just need to place the characters to be grouped inside a set of parentheses. For example −(.*)(\d+)(.*)If you are trying to match multiple groups the match results of each group is captured. You can get the results a group by passing its respective group number to the group() method. 1, 2, 3 etc.. (from right to left) group 0 indicates the whole match.Example Live Demoimport java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; public class CapturingGroups {    public static void main( String args[] ) {       System.out.println("Enter input ... Read More

Matching a whole word Java Regular expressions:

Maruthi Krishna
Updated on 13-Jan-2020 06:12:00

2K+ Views

The meta character "\b" matches word boundaries. i.e. it matches before the first and after the last word characters and between word and non-word characters.Therefore to match a whole word you need to surround it between the word boundary meta characters as −\btest\bExample Live DemoFollowing Java example counts and prints the number of occurrences of the word test in the given input string.import java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; public class RegexExample1 {    public static void main(String[] args) {       Scanner sc = new Scanner(System.in);       System.out.println("Enter input text: ");       String input = sc.nextLine(); ... Read More

Matching from a set of characters Java regualr expression

Maruthi Krishna
Updated on 13-Jan-2020 06:09:00

132 Views

The character classes in Java regular expression is defined using the square brackets "[ ]", the character class matches a single character from the specified or, set of possible characters.For example, the regular expression [abc] matches a single character a or, b or, c. Similarly, "[a-z]" matches a character from a to z.Example 1 Live Demoimport java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; public class RegexExample1 {    public static void main(String[] args) {       Scanner sc = new Scanner(System.in);       System.out.println("Enter input text: ");       String input = sc.nextLine();       String regex = "[a-z]"; ... Read More

Matching Nonprintable Characters using Java regex

Maruthi Krishna
Updated on 13-Jan-2020 06:06:18

1K+ Views

There are 7 common non printable characters used in general and each character has its own hexadecimal representation.NamecharactersHexa-decimal representationbell\a0x07Escape\e0x1BForm feed\f0x0CLine feed0x0ACarriage return\r0X0DHorizontal tab\t0X09Vertical tab\v0X0BExample 1 Live DemoFollowing Java program accepts an input text and counts the number of tab spaces in it −import java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; public class RegexExample1 {    public static void main(String[] args) {       Scanner sc = new Scanner(System.in);       System.out.println("Enter input text: ");       String input = sc.nextLine();       String regex = "\t";       //Creating a pattern object       Pattern pattern = ... Read More

Advertisements