• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 199
  • Last Modified:

Java Regex Question

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
ecuguru
Asked:
ecuguru
1 Solution
 
ozoCommented:
       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
 
zzynxSoftware engineerCommented:
>> 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
 
SprudeVICommented:
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
 
SprudeVICommented:

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
 
ecuguruAuthor Commented:
Perfect, sorry about the delay in assigning.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now