Found 2616 Articles for Java

Matching from a set of characters Java regualr expression

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

84 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

829 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

Difference between Java and C language

Mahesh Parahar
Updated on 24-Feb-2020 08:06:43

639 Views

Both Java and C are the most popular programming languages in programming world.Due to their various important characteristics and features both of these languages are widely used globally.On the basis of their features following are the important differences between Java and CSr. No.KeyJavaC1IntroducedJava was developed after C as compared on introducing year.It was developed by James Gosling in 1995.On other hand C was introduced before Java and was developed by Dennis M. Ritchie between 1969 and 1973.2TypeJava is a high level language and is more data oriented also known globally as Object-Oriented language.On other hand C is a middle-level language ... Read More

Regular expression “[X?+] ” Metacharacter Java

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

138 Views

The Possessive Quantifier [X?+] matches the X present once or not present at all.Example Live Demopackage com.tutorialspoint; import java.util.regex.Matcher; import java.util.regex.Pattern; public class PossesiveQuantifierDemo {    private static final String REGEX = "T?+";    private static final String INPUT = "abcdTatW";    public static void main(String[] args) {       // create a pattern       Pattern pattern = Pattern.compile(REGEX);       // get a matcher object       Matcher matcher = pattern.matcher(INPUT);       while(matcher.find()) {          //Prints the start index of the match.          System.out.println("Match String start(): "+matcher.start()); ... Read More

Counting the number of groups Java regular expression

Maruthi Krishna
Updated on 13-Jan-2020 05:59:52

902 Views

You can treat multiple characters as a single unit by capturing them as groups. You just need to place these characters inside a set of parentheses.You can count the number of groups in the current match using the groupCount() method of the Matcher class. This method calculates the number of capturing groups in the current match and returns it.Example Live Demoimport java.util.regex.Matcher; import java.util.regex.Pattern; public class Test {    public static void main(String[] args) {       String str1 = "This is an example HTML script where ever alternative word is bold.";       //Regular expression to match contents ... Read More

Explain character classes in Java regular expressions

Deepti S
Updated on 29-Aug-2023 15:47:34

254 Views

Java regular expressions, is often known as rege. It is employed for searching and manipulating text. They are utilized in numerous applications. Online scraping, email screening, and password validation are a few of them.A pattern that can be employed to match a specific character sequence in a string is also known as a regular expression. To build the pattern, a special syntax involving quantifiers, character classes, wildcard characters, and ordinary characters is employed. Character classes are characters that can be matched by a regular expression. They are defined using square brackets []. For instance, the character class [abc] matches the ... Read More

Reluctant quantifiers Java Regular expressions

Maruthi Krishna
Updated on 13-Jan-2020 05:53:17

744 Views

Greedy quantifiers are the default quantifiers. A greedy quantifier matches as much as possible from the input string (longest match possible) if match not occurred it leaves the last character and matches again.Whereas a reluctant or, non-greedy quantifier matches as little as possible, initially the non-greedy quantifier matches the first character if match not occurred it adds another character from the input string and tries to match.If you place a "?" after a greedy quantifier it becomes reluctant or non-greedy quantifier. Following is the list of reluctant quantifiers −QuantifierDescriptionre*?Matches zero or more occurrences.re??Matches zero or, 1 occurrence.re+?Matches one or more ... Read More

Java regex program to match parenthesis "(" or, ")".

Maruthi Krishna
Updated on 21-Feb-2020 11:27:01

5K+ Views

Following regular expression accepts a string with parenthesis −"^.*[\(\)].*$";^ matches the starting of the sentence..* Matches zero or more (any) characters.[\(\)] matching parenthesis.$ indicates the end of the sentence.Example 1 Live Demoimport java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; public class SampleTest {    public static void main( String args[] ) {       String regex = "^.*[\(\)].*$";       //Reading input from user       Scanner sc = new Scanner(System.in);       System.out.println("Enter data: ");       String input = sc.nextLine();       //Instantiating the Pattern class       Pattern pattern = Pattern.compile(regex);     ... Read More

Explain quantifiers in Java regular expressions

Deepti S
Updated on 29-Aug-2023 16:31:53

236 Views

Quantifiers in Java are special characters that allow you to specify the number of times a character or group of characters can occur in a regular expression. The most common quantifiers are: *: One or more instances of the character or set of characters that came before it. ?: The character or set of characters before it, either zero or one instance. +: One or more instances of the character or collection of characters that came before it. {n}: Specifically n instances of the character or set of characters that came before it. {n, }: The character or set ... Read More

Posix character classes p{Blank} Java regex

Maruthi Krishna
Updated on 21-Feb-2020 11:11:04

185 Views

This class matches all tabs or spaces.Example  Live Demoimport java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; public class PrintableCharacters {    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 = "[\p{Blank}]";       //Compiling the regular expression       Pattern pattern = Pattern.compile(regex);       //Retrieving the matcher object       Matcher matcher = pattern.matcher(input);       int count = 0;   ... Read More

Advertisements