Programming Articles - Page 3237 of 3366

Regular Expressions syntax in Java Regex

Jai Janardhan
Updated on 30-Jul-2019 22:30:21

287 Views

Following is a simple program demonstrating how to use regular expression in Java. Java Regex Characters

Regex named groups in Java

George John
Updated on 30-Jul-2019 22:30:21

303 Views

Java Regex Capturing Groups

Named Capturing groups in Java Regex

Arushi
Updated on 30-Jul-2019 22:30:21

199 Views

Java Regex Capturing Groups

Capturing groups and back references in Java Regex

Moumita
Updated on 25-Feb-2020 07:11:20

1K+ Views

Capturing groups are a way to treat multiple characters as a single unit. They are created by placing the characters to be grouped inside a set of parentheses. For example, the regular expression (dog) creates a single group containing the letters "d", "o", and "g".Capturing groups are numbered by counting their opening parentheses from the left to the right.In the expression ((A)(B(C))), for example, there are four such groups -((A)(B(C)))(A)(B(C))(C)Back references allow repeating a capturing group using a number like \# where # is the groupnumber. See the example below −ExampleLive Demoimport java.util.regex.Matcher; import java.util.regex.Pattern; public class Tester {   ... Read More

How do I use capturing groups in Java Regex?

Paul Richard
Updated on 30-Jul-2019 22:30:21

142 Views

https://www.tutorialspoint.com/javaregex/javaregex_capturing_groups.htm

How to match a line not containing a word in Java Regex

Arnab Chakraborty
Updated on 21-Jun-2020 06:31:20

465 Views

ExampleLive Demoimport java.util.regex.Matcher; import java.util.regex.Pattern; public class NoRegTest {    public static void main(String[] args) {       String s="^fun";       Pattern pattern = Pattern.compile(s);       Matcher matcher = pattern.matcher("Java is fun");       if(!matcher.find()) {          System.out.println("not found");       }    } }Outputnot found

Java Regular Expression that doesn't contain a certain String.

Arnab Chakraborty
Updated on 20-Jun-2020 10:34:58

656 Views

Exampleimport java.util.regex.*; class PatternMatch{    public static void main(String args[]) {       String content = "I am a student";       String string = ".*boy.*";       boolean isMatch = Pattern.matches(string, content);       System.out.println("The line contains 'boy'?"+ isMatch);    } }Outputthe line contains 'boy'?falsematches()­­It is used to check if the whole text matches a pattern. Its output is boolean. It returns true if match is found otherwise false.This is one of simplest and easiest way of searching a String in a text using Regex .There is a another method compile() , if you want ... Read More

How to use R in Java-8 regex.

Arnab Chakraborty
Updated on 30-Jul-2019 22:30:22

506 Views

\R matches any line break as defined by the Unicode standardPattern p = Pattern.compile("\R");Unicode line-break sequence is equivalent to \u000D\u000A|[\u000A\u000B\u000C\u000D\u0085\u2028\u2029]

Why we should use whole string in Java regular expression

Arnab Chakraborty
Updated on 21-Jun-2020 14:14:42

1K+ Views

In Java regex matches() matches the input string against the whole string as it add a ^ and $ at the end of the input string.so it will not match the substring. So for matching substring, you should use find().Exampleimport java.util.regex.*; class PatternMatchingExample {    public static void main(String args[]) {       String content = "aabbcc";       String string = "aa";       Pattern p = Pattern.compile(string);       Matcher m = p.matcher(content);       System.out.println(" 'aa' Match:"+ m.matches());       System.out.println(" 'aa' Match:"+ m.find());    } }Output'aa' Match:false 'aa' Match:true

Regex to match lines containing multiple strings in Java

Arnab Chakraborty
Updated on 21-Jun-2020 06:33:07

835 Views

ExampleLive Demoimport java.util.ArrayList; import java.util.List; import java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; public class SearchRegex {    private Pattern subPattern = Pattern.compile(SUBJECT_PATTERN);    private Matcher matcher;    private static final String SUBJECT_PATTERN = "(?s)Subject 1:\s(.*)Subject 2:";    public static void main(String[] args) {       String d = "Subject 1: Java" + "Subject 2: Python";       SearchRegex obj = new SearchRegex();       List list = obj.getSubject(d);       System.out.println("Address Result : " + list);    }    private List getSubject(String d){       List result = new ArrayList();       matcher = subPattern.matcher(d);       while (matcher.find()) {          result.add(matcher.group(1));       }       return result;    } }OutputAddress Result : [Java]

Advertisements