Found 4338 Articles for Java 8

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

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

223 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

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

874 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

189 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

184 Views

Java Regex Capturing Groups

Named Capturing groups in Java Regex

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

118 Views

Java Regex Capturing Groups

Capturing groups and back references in Java Regex

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

710 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

91 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

261 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

454 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

Advertisements