Programming Articles - Page 2156 of 3363

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

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

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

Difference between Structure and Array in C

Mahesh Parahar
Updated on 25-Feb-2020 07:13:01

7K+ Views

In C both Structure and Array are used as container for data types i.e in both structure and array we can store data and also can perform different operations over them.On the basis of internal implementation following are some basic differences between both.Sr. No.KeyStructureArray1DefinitionStructure can be defined as a data structure used as container which can hold variables of different types.On other hand Array is a type of data structure used as container which can hold variables of same type and do not support multiple data type variables.2Memory AllocationMemory allocation for input data in structure does not necessary to be ... Read More

Difference between strlen() and sizeof() for string in C Program

Mahesh Parahar
Updated on 25-Feb-2020 06:57:24

366 Views

As we know that in programming string can be defined as the collection of characters. Now for the requirement of finding how many characters are being used to create a string, C provides two approaches which are strlen() and sizeof().As mentioned in above point both of these methods are used to find out the length of target operand but on the basis of their internal implementation following are some basic differences between both.Sr. No.Keystrlen()sizeof()1Definitionstrlen() is a predefined function defined in a Header file named string.h in C.On other hand sizeof() is a Unary operator and not a predefined function.2Implementationstrlen is ... Read More

Explain quantifiers in Java regular expressions

Alshifa Hasnain
Updated on 12-Jun-2025 17:31:35

739 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. ... Read More

Posix character classes p{Blank} Java regex

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

328 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

Possessive quantifiers Java Regular expressions

Maruthi Krishna
Updated on 10-Jan-2020 12:46:14

451 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.A possessive quantifier is similar to a greedy quantifier the only difference is it tries to match as many character as it can initially and, if match not occurred unlike greedy quantifier it does not backtrack.If you place a "+" after a greedy quantifier it becomes possessive quantifier. Following is the list of possessive quantifiers −QuantifierDescriptionre*+Matches zero or more occurrences.re?+Matches zero or, 1 occurrence.re++Matches one or more occurrences.re{n}+Matches ... Read More

Java program to remove all numbers in a string except "1" and "2"?

Maruthi Krishna
Updated on 10-Jan-2020 12:44:08

263 Views

The regular expression "(?digit(?!\d)" matches the digit specified.The replaceAll() method accepts two strings: a regular expression pattern and, the replacement string and replaces the pattern with the specified string.Therefore, to remove all numbers in a string except 1 and 2, replace the regular expressions 1 and 2 with one and two respectively and replace all the other digits with an empty string.Example Live Demoimport java.util.Scanner; public class RegexExample {    public static void main(String args[]) {       //Reading String from user       System.out.println("Enter a String");       Scanner sc = new Scanner(System.in);       String ... Read More

Regular Expression Q Metacharacter in Java

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

1K+ Views

The subexpression/metacharacter "\Q" escapes all characters up to "\E" i.e. you can escape metacharacters in the regular expressions by placing them in between \Q and \E. For example, the expression [aeiou] matches the strings with vowel letters in it.Example  Live Demoimport java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; public class SampleProgram {    public static void main( String args[] ) {       String regex = "[aeiou]";       Scanner sc = new Scanner(System.in);       System.out.println("Enter input string: ");       String input = sc.nextLine();       //Creating a Pattern object       Pattern pattern = ... Read More

Character class: subtraction - Java regular expressions

Maruthi Krishna
Updated on 10-Jan-2020 12:38:17

531 Views

You can subtract one range from other and use it as new range. You can achieve this by using two variants of character classes i.e. negation and intersection.For example the intersection of ranges [a-l] and [^e-h] gives you the characters a to l as rage subtracting the characters [e-h]Example 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-l&&[^e-h]]";       //Creating a ... Read More

Character class: intersection - Java regular expressions

Maruthi Krishna
Updated on 10-Jan-2020 12:35:27

342 Views

The character classes in Java regular expression is defined using the square brackets "[ ]", this subexpression 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.The intersection variant of the character class allows you to match a character which is common in the ranges that have intersection relation between them.An intersection relation between ranges is defined using && i.e. the expression [a-z&&[r-u]] matches a single character from r to u.Example Live Demoimport java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; public class RegexExample1 {    public ... Read More

Advertisements