Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Java Regex Question

Posted on 2008-06-24
5
Medium Priority
?
195 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 2000 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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
Suggested Courses

609 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