Programming Articles - Page 2154 of 3363

Matching multiple lines in Java regular expressions

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

976 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.Example Live Demoimport java.util.regex.Matcher; import java.util.regex.Pattern; public class MatchingText{    public static void main(String[] args) {       String input = ... Read More

Difference between the and$ operator in php

Mahesh Parahar
Updated on 13-Jan-2020 06:43:02

523 Views

$ operatorOperator is used to define variables in php. For example, message. Such variables can contain any type of value like int, string, etc.$$ operator$$ is a special operator that contains the name of another variable and can be used to access the value of that variable.ExampleFollowing the example, shows the usage of '′vs′$' operators. Live Demo    PHP Example     Outputmessage Welcome to Tutorialspoint

Splitting the text using java.util.regex package

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

169 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

Difference between the AND and && operator in php

Mahesh Parahar
Updated on 13-Jan-2020 06:39:02

451 Views

'AND' Logical operator'AND' operator is a logical AND operator but has low precedence to = operator.'&&' Logical operator'&&' is also a logical AND operator but has high precedence to = operator.ExampleFollowing the example, shows the difference of 'AND' vs '&&' operators. Live Demo    PHP Example     Output$result = $x AND $y bool(true) $result = $x && $y bool(false)

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

Difference between the | and || or operator in php

Mahesh Parahar
Updated on 13-Jan-2020 06:35:31

390 Views

'|' Bitwise OR operator'|' operator is a bitwise OR operator and is used to set the bit to 1 if any of the corresponding bit is 1.'||' Logical Or operator'||' is a logical Or operator and works on complete operands as whole.ExampleFollowing example, shows usage of '|' vs '||' operators. Live Demo    PHP Example     Output$x | $y = 3 $x || $y = 1

Replacing all the matched contents Java regular expressions

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

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 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

320 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

Difference between !== and ==! operator in PHP

Mahesh Parahar
Updated on 13-Jan-2020 06:33:04

582 Views

'!==' comparison operator'!==' operator checks the unequality of two objects with a type check. It does not converts the datatype and makes a typed check.For example 1 !== '1' will results true.'==!' comparison operator'==!' operator is combination of two operators and can be written as == (!operands).ExampleFollowing example, shows usage of '!==' vs '==!' operators. Live Demo    PHP Example     Output$x !== operator $y = bool(true) $x ==! operator $y = bool(true)

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

Advertisements