Click here to watch on Youtube :
https://www.youtube.com/watch?v=NZVtYrycZ-k&list=UUhwKlOVR041tngjerWxVccw
RegexDemo.java
Output
Click the below link to download the code:
https://sites.google.com/site/ramj2eev1/home/javabasics/RegexDemo_nested_group_john.zip?attredirects=0&d=1
Github Link:
https://github.com/ramram43210/Java/tree/master/BasicJava/RegexDemo_nested_group_john
Bitbucket Link:
https://bitbucket.org/ramram43210/java/src/57d5e7f2c5fabff367c40b36a92925e1158e7f5a/BasicJava/RegexDemo_nested_group_john/?at=master
See also:
All JavaEE Videos Playlist
All JavaEE Videos
All JAVA EE Links
Servlets Tutorial
All Design Patterns Links
JDBC Tutorial
Java Collection Framework Tutorial
JAVA Tutorial
Kids Tutorial
https://www.youtube.com/watch?v=NZVtYrycZ-k&list=UUhwKlOVR041tngjerWxVccw
RegexDemo.java
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* Groups Inside Groups
*
* It is possible to have groups inside group in a regular
* expression.
*
*/
public class RegexDemo
{
public static void main(String[] args)
{
String text = "John writes about this, and John Doe writes about that,"
+ " and John Wayne writes about everything.";
/*
* Notice how the two groups from the examples earlier are now
* nested inside a larger group. (again, you cannot see the
* space at the end of the expression, but it is there).
*
* When groups are nested inside each other, they are numbered
* based on when the left paranthesis of the group is met.
* Thus, group 1 is the big group. Group 2 is the group with
* the expression John inside. Group 3 is the group with the
* expression .+? inside. This is important to know when you
* need to reference the groups via the groups(int groupNo)
* method.
*/
String regex = "((John) (.+?)) ";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(text);
while (matcher.find())
{
System.out.println("Group1 = "+matcher.group(1));
System.out.println("Group2 = "+matcher.group(2));
System.out.println("Group3 = "+matcher.group(3));
System.out.println("--------------------------------");
}
}
}
import java.util.regex.Pattern;
/**
* Groups Inside Groups
*
* It is possible to have groups inside group in a regular
* expression.
*
*/
public class RegexDemo
{
public static void main(String[] args)
{
String text = "John writes about this, and John Doe writes about that,"
+ " and John Wayne writes about everything.";
/*
* Notice how the two groups from the examples earlier are now
* nested inside a larger group. (again, you cannot see the
* space at the end of the expression, but it is there).
*
* When groups are nested inside each other, they are numbered
* based on when the left paranthesis of the group is met.
* Thus, group 1 is the big group. Group 2 is the group with
* the expression John inside. Group 3 is the group with the
* expression .+? inside. This is important to know when you
* need to reference the groups via the groups(int groupNo)
* method.
*/
String regex = "((John) (.+?)) ";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(text);
while (matcher.find())
{
System.out.println("Group1 = "+matcher.group(1));
System.out.println("Group2 = "+matcher.group(2));
System.out.println("Group3 = "+matcher.group(3));
System.out.println("--------------------------------");
}
}
}
Output
Group1 = John writes
Group2 = John
Group3 = writes
--------------------------------
Group1 = John Doe
Group2 = John
Group3 = Doe
--------------------------------
Group1 = John Wayne
Group2 = John
Group3 = Wayne
--------------------------------
Group2 = John
Group3 = writes
--------------------------------
Group1 = John Doe
Group2 = John
Group3 = Doe
--------------------------------
Group1 = John Wayne
Group2 = John
Group3 = Wayne
--------------------------------
Click the below link to download the code:
https://sites.google.com/site/ramj2eev1/home/javabasics/RegexDemo_nested_group_john.zip?attredirects=0&d=1
Github Link:
https://github.com/ramram43210/Java/tree/master/BasicJava/RegexDemo_nested_group_john
Bitbucket Link:
https://bitbucket.org/ramram43210/java/src/57d5e7f2c5fabff367c40b36a92925e1158e7f5a/BasicJava/RegexDemo_nested_group_john/?at=master
See also:
No comments:
Post a Comment