Java Articles

Page 26 of 450

Finding a Match Within Another Match Java regular expressions

Maruthi Krishna
Maruthi Krishna
Updated on 11-Mar-2026 338 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.Exampleimport 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

Replacing all the matched contents Java regular expressions

Maruthi Krishna
Maruthi Krishna
Updated on 11-Mar-2026 2K+ 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 1import 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

Named captured groups Java regular expressions

Maruthi Krishna
Maruthi Krishna
Updated on 11-Mar-2026 3K+ Views

Named capturing groups allows you to reference the groups by names. Java started supporting captured groups since SE7.Exampleimport 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

Matching multiple lines in Java regular expressions

Maruthi Krishna
Maruthi Krishna
Updated on 11-Mar-2026 1K+ Views

To match/search a input data with multiple lines −Get the input string.Split it into an array of tokens by passing "\r?" as parameter to the split method.Compile the required regular expression using the compile() method of the pattern class.Retrieve the matcher object using the matcher() method.In the for loop find matches in the each element (new line) of the array using the find() method.Reset the input of the matcher to the next element of the array using the reset() method.Exampleimport java.util.regex.Matcher; import java.util.regex.Pattern; public class MatchingText{    public static void main(String[] args) {       String input = "sample ...

Read More

Print all permutation of a string using ArrayList in Java

sudhir sharma
sudhir sharma
Updated on 11-Mar-2026 830 Views

In this problem, we are given a string of size n and we have to print all permutations of the string. But this time we have to print this permutation using ArrayList.Let’s take an example to understand the problem -Input − string = ‘XYZ’Output − XYZ, XZY, YXZ, YZX, ZXY, ZYXTo solve this problem, we will be generating all permutations of the character of the string. We will use a recursive function and will return arrayList.ExampleThe following is ArrayList implementation of the algorithm −import java.util.ArrayList; public class Main{    static void printArrayList(ArrayList combo) {       combo.remove("");     ...

Read More

Java regex program to verify whether a String contains at least one alphanumeric character.

Maruthi Krishna
Maruthi Krishna
Updated on 11-Mar-2026 8K+ Views

Following regular expression matches a string that contains at least one alphanumeric characters −"^.*[a-zA-Z0-9]+.*$";Where, ^.* Matches the string starting with zero or more (any) characters.[a-zA-Z0-9]+ Matches at least one alpha-numeric character..*$ Matches the string ending with zero or more (ant) characters.Example 1import java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; public class Example {    public static void main(String args[]) {       //Reading String from user       System.out.println("Enter a string");       Scanner sc = new Scanner(System.in);       String input = sc.nextLine();       //Regular expression       String regex = "^.*[a-zA-Z0-9]+.*$";     ...

Read More

BinaryOperator Interface in Java

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 1K+ Views

The BinaryOperator interface represents an operation upon two operands of the same type, producing a result of the same type as the operands.Following are the methods −Modifier and TypeMethod and DescriptionmaxBy(Comparator

Read More

Java regex program to add space between a number and word in Java.

Maruthi Krishna
Maruthi Krishna
Updated on 11-Mar-2026 2K+ Views

You can form matched groups in the regular expression by separating the expressions with parenthesis. In following regular expression the first group matches digits and the second group matches the English alphabet −(\d)([A-Za-z])In short, it matches the part in the input string where a digit followed by an alphabet.Since the expression $1 indicates Group1 and $2 indicates Group2, if you replace The above Java regular expression with $1 $2, using the replace() method (of the String class) a space will be added between number and a word in the given input string when a number is followed by a word.Exampleimport ...

Read More

How to get Exception log from a console and write it to external file in java?

Maruthi Krishna
Maruthi Krishna
Updated on 11-Mar-2026 1K+ Views

There are several logging frame works available to log your data in to files. You can also define your own method.Example − Using I/O packageFollowing Java program has an array storing 5 integer values, we are letting the user to choose two elements from the array (indices of the elements) and performing division between them. We are wrapping this code in try block with three catch blocks catching ArithmeticException, InputMismatchException and, ArrayIndexOutOfBoundsException. In each of them we are invoking the writeToFile() method.This method accepts an exception object, and appends it to a file using the write() method of the Files ...

Read More

Can we to override a catch block in java?

Maruthi Krishna
Maruthi Krishna
Updated on 11-Mar-2026 1K+ Views

DescriptionWhen a piece of code in particular method throws an exception, and is handled using try-catch pair. If we are calling this method from another one and, the calling line is wrapped within try-catch pair. Now, how can I override the catch block by the catch block of the calling method.When a piece of code in a method throws an exception (compile time) we must either handle it by wrapping it within the try-catch pair or, throw it (postpone) to the calling method using the throws keyword else a compile time error occurs.In the following Java example the code in ...

Read More
Showing 251–260 of 4,498 articles
« Prev 1 24 25 26 27 28 450 Next »
Advertisements