Solved

Java Regex Question

Posted on 2008-06-24
5
187 Views
Last Modified: 2013-11-24
I'd like to be able to see if a pattern exists.
If so, I'd like to see if ANOTHER pattern encapsulates it.  It won't always, but if it does, I'd like to detect it.

blah blah {{Pattern1[[Value]]}} blah blah [[Value2]].

So if the [[]] is found, cool.  I can do this with pattern matcher.  But I don't know how to see if it's inside of an additional pattern?  So in the above, if [[ ]] is found great.  Is there a {{}} around it?

Thanks!
0
Comment
Question by:ecuguru
5 Comments
 
LVL 84

Expert Comment

by:ozo
ID: 21861362
       System.out.println("blah blah {{Pattern1[[Value]]}} blah blah [[Value2]]".matches(".*Pattern1\\[\\[Value\\]\\].*"));
        System.out.println("blah blah {{Pattern1[[Value]]}} blah blah [[Value2]]".matches(".*\\{\\{Pattern1\\[\\[Value]]}}.*"));
0
 
LVL 37

Expert Comment

by:zzynx
ID: 21863281
>> if [[ ]] is found great.  Is there a {{}} around it?

String regEx = ".*\\{\\{.*\\[\\[.*]]}}.*";
System.out.println("blah blah {{Pattern1[[Value]]}} blah blah [[Value2]]".matches(regEx)); // Match
System.out.println("blah blah Pattern1[[Value]] blah blah [[Value2]]".matches(regEx));         // No Match
0
 
LVL 7

Expert Comment

by:SprudeVI
ID: 22087102
Use groups to determine what you have found. In reg. expressions groups are enclosed in brackets. Later you can use the "String Matcher.group(int grNumber)" method to get the string matched by the group.
import java.util.regex.Matcher;
import java.util.regex.Pattern;
 
public class Test {
  public static void main(String[] args)  {
    String s = "blah blah {{Pattern1[[Value]]}} blah blah [[Value2]]";
    
    Pattern pattern = Pattern.compile(
      "(Pattern1\\[\\[(.+?)\\]\\])|(\\[\\[(.+?)\\]\\])");
    //   ^        ^^^   ^            ^      ^
    //   |         |    |            |      |
    //   |         |    |            |      +-- group 4
    //   |         |    |            +-- group 3  
    //   |         |    +-- group 2  
    //   |         +-- rect. brackets have to be quoted  
    //   +-- group 1
    Matcher matcher = pattern.matcher(s);
    while (matcher.find()) {
      if (matcher.group(2) != null) {
        System.out.println("Value found inside pattern!");
        System.out.println("> Pattern: " + matcher.group(1));
        System.out.println("> Value: " + matcher.group(2));
        System.out.println();
      } else if (matcher.group(3) != null) {
        System.out.println("Value found without pattern!");
        System.out.println("> Value: " + matcher.group(4));
        System.out.println();
      }
    }
  }
}

Open in new window

0
 
LVL 7

Accepted Solution

by:
SprudeVI earned 500 total points
ID: 22087108

import java.util.regex.Matcher;
import java.util.regex.Pattern;
 
public class Test {
  public static void main(String[] args)  {
    String s = "blah blah {{Pattern1[[Value]]}} blah blah [[Value2]]";
    
    Pattern pattern = Pattern.compile(
      "(Pattern1\\[\\[(.+?)]])|(\\[\\[(.+?)]])");
    //   ^      ^^^   ^        ^      ^
    //   |       |    |        |      |
    //   |       |    |        |      +-- group 4
    //   |       |    |        +-- group 3  
    //   |       |    +-- group 2  
    //   |       +-- rect. brackets have to be quoted  
    //   +-- group 1
    Matcher matcher = pattern.matcher(s);
    while (matcher.find()) {
      if (matcher.group(2) != null) {
        System.out.println("Value found inside pattern!");
        System.out.println("> Pattern: " + matcher.group(1));
        System.out.println("> Value: " + matcher.group(2));
        System.out.println();
      } else if (matcher.group(3) != null) {
        System.out.println("Value found without pattern!");
        System.out.println("> Value: " + matcher.group(4));
        System.out.println();
      }
    }
  }
}

Open in new window

0
 

Author Closing Comment

by:ecuguru
ID: 31470407
Perfect, sorry about the delay in assigning.
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
V.simple question about Android packages and libraries 1 20
micro services spring boot application error 3 194
eclipse argument 14 61
eclipse console opening separately 2 21
For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question