Programming Articles - Page 2155 of 3363

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

143 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

Regular expression “[X?+] ” Metacharacter Java

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

218 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

1K+ 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

459 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

Difference between float and double in C/C++

Mahesh Parahar
Updated on 24-Feb-2020 08:04:39

2K+ Views

As we know that in C/C++ we require float and double data type for the representation of Floating point numbers i.e the numbers which have decimal part with them.Now on the basis of precision provided by both of these data types we can differentiate between both of them.In simple words it could be state that double has 2x more precision as compare than float which means that double data type has double precision than as compare to that of float data type.In terms of number of precision it can be stated as double has 64 bit precision for floating point ... Read More

Reluctant quantifiers Java Regular expressions

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

992 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

Advertisements