Found 9150 Articles for Object Oriented Programming

How to extract a group from a Java String that contains a Regex pattern

Arnab Chakraborty
Updated on 21-Jun-2020 06:30:13

311 Views

How to extract a group from a Java String that contains a Regex patternimport java.util.regex.Matcher; import java.util.regex.Pattern; public class RegexTest {    public static void main(String[] args) {       Pattern pattern = Pattern.compile("fun");       Matcher matcher = pattern.matcher("Java is fun");       // using Matcher find(), group(), start() and end() methods       while (matcher.find()) {          System.out.println("Found the text \"" + matcher.group()             + "\" starting at " + matcher.start()             + " index and ending at index ... Read More

How to capture multiple matches in the same line in Java regex

Arnab Chakraborty
Updated on 20-Jun-2020 10:49:20

3K+ Views

Exampleimport java.util.regex.*; class PatternMatcher {    public static void main(String args[]) {       int count = 0;       // String to be scanned to find the pattern.       String content = "aaa bb aaa";       String string = "aaa";       // Create a Pattern object       Pattern p = Pattern.compile(string);       // get a matcher object       Matcher m = p.matcher(content);       while(m.find()) {          count++;          System.out.println("Match no:"+count);         ... Read More

Search and Replace with Java regular expressions

Sravani S
Updated on 26-Feb-2020 08:09:33

1K+ Views

Java provides the java.util.regex package for pattern matching with regular expressions. Java regular expressions are very similar to the Perl programming language and very easy to learn.A regular expression is a special sequence of characters that help you match or find other strings or sets of strings, using a specialized syntax held in a pattern. They can be used to search, edit, or manipulate text and data.The replaceFirst() and replaceAll() methods replace the text that matches a given regular expression. As their names indicate, replaceFirst replaces the first occurrence, and replaceAll replaces all occurrences.ExampleLive Demoimport java.util.regex.Matcher; import java.util.regex.Pattern; public class RegexMatches { ... Read More

Regular Expressions syntax in Java Regex

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

281 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

293 Views

Java Regex Capturing Groups

Named Capturing groups in Java Regex

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

191 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

139 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

452 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 regex to exclude a specific String constant

Arnab Chakraborty
Updated on 29-Sep-2024 02:51:09

2K+ Views

In this program, we will use a regular expression to check if a given string does not contain the substring "kk" using Java. The regular expression ^((?!kk).)*$ is designed to match strings that do not include the "kk" pattern anywhere in the string. The program will evaluate a sample string and print whether or not it contains "kk". Problem Statement Write a program in Java for checking whether a given string contains the substring "kk." If the string does not contain "kk" the program will return true otherwise it will return false. Input String s = "tutorials" Output true ... Read More

Advertisements