1

I am not familiar with Patterns & matchers, and I am pretty stuck with this problem.

I have a string that I would like to manipulate in Java. I understand that I have to use

Pattern p = Pattern.compile("\\[(.*?)\\]");
Matcher m = p.matcher(in);

while(m.find()) {
     String found = m.group(1).toString();
}

But in my string, let's say I have the following examples:

String in = "test anything goes here [A#1234|567]"; //OR
String in = "test anything goes here [B#1234|567]"; //OR
String in = "test anything goes here [C#1234|567]";

I want to find [A# | ] or [B# | ] or [C# | ] in the string, how do I use the regex to find the expression?

9
  • do you want to find that | also, along with spaces? Commented Mar 10, 2014 at 13:25
  • 1
    Unclear if you want the numbers after A#, etc Commented Mar 10, 2014 at 13:25
  • It seems you want to replace "[0-9]+" to "". :) Commented Mar 10, 2014 at 13:27
  • 1
    Regex you are using will find these values for you. What exactly is problem you are facing? Commented Mar 10, 2014 at 13:28
  • @Ryven, yes, but the numbers should be "dynamic". I can have things like [A#656464566|874834637647], numbers can vary. Commented Mar 10, 2014 at 13:28

4 Answers 4

1

Use [ABC]# in your regex to match your expression.

Pattern p = Pattern.compile("(\\[[ABC]#.*?\\])");

If the fields are digit then you can safely use \d+

Pattern p = Pattern.compile("(\\[[ABC]#\\d+\\|\\d+\\])");
Sign up to request clarification or add additional context in comments.

2 Comments

How about the "|"? That would need to be present too... Is it possible to get that in?
@progricebowl I already updated the answer. Please have a look.
1

I'd use a simple Pattern as in the following example:

String[] in = { "test anything goes here [A#1234|567]",
            "test anything goes here [B#1234|567]",
            "test anything goes here [C#1234|567]" };

    Pattern p = Pattern.compile("\\[[A-Z]#\\d+\\|\\d+\\]");
    for (String s: in) {
        Matcher m = p.matcher(s);
        while (m.find()) {
            System.out.println("Found: " + m.group());
        }
    }
}

Output

Found: [A#1234|567]
Found: [B#1234|567]
Found: [C#1234|567]

I'm assuming here that your Pattern has specific restrictions:

  • Starts with [
  • Followed by one upper-case non-accented letter
  • Followed by #
  • Followed by any number of digits
  • Followed by |
  • Followed by any number of digits
  • Followed by ]

Comments

0

Try:

Pattern p = Pattern.compile("(\\[[A-Z]#.*\\])");

If you want to match any capital A through Z. Unclear if you want all the data between [] though.

Comments

0

My solution:

import java.util.*;
import java.lang.*;
import java.io.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

class Some {
    public static void main(String[] args) throws java.lang.Exception {
        String[] in = {
            "test anything goes here [A#1234|567]",
                "test anything goes here [B#1234|567]",
                "test anything goes here [C#1234|567]"
        };

        Pattern p = Pattern.compile("\\[(.*?)\\]");
        for (String s: in ) {
            Matcher m = p.matcher(s);
            while (m.find()) {
                System.out.println("Found: " + m.group().replaceAll("\\d", ""));
            }
        }

    }
}

This uses your original regex.

Demo: http://ideone.com/4Z5oYD

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.