0

I posted this question a couple weeks ago pertaining to extracting a capture group using regex in Java, Extracting Capture Group Using Regex, and I received a working answer. I also posted this question a couple weeks ago pertaining to character replacement in Java using regex, Replace Character in Matching Regex, and received an even better answer that was more dynamic than the one I got from my first post. I'll quickly illustrate by example. I have a string like this that I want to extract the "ID" from:

String idInfo = "Any text up here\n" +
                "Here is the id\n" +
                "\n" +
                "?a0 12 b5\n" +
                "&Edit Properties...\n" +
                "And any text down here";

And in this case I want the output to just be:

a0 12 b5

But it turns out the ID could be any number of octets (just has to be 1 or more octets), and I want my regex to be able to basically account for an ID of 1 octet then any number of subsequent octets (from 0 to however many). The person I received an answer from in my Replace Character in Matching Regex post did this for a similar but different use case of mine, but I'm having trouble porting this "more dynamic" regex over to the first use case.

Currently, I have ...

Pattern p = Pattern.compile("(?s)?:Here is the id\n\n\\?([a-z0-9]{2})|(?<!^)\\G:?([a-z0-9]{2})|.*?(?=Here is the id\n\n\\?)|.+");
Matcher m = p.matcher(certSerialNum);
String idNum = m.group(1);
System.out.println(idNum);

But it's throwing an exception. In addition, I would actually like it to use all known adjacent text in the pattern including "Here is the id\n\n\?" and "\n&Edit Properties...". What corrections do I need to get this working?

Community
  • 1
  • 1
user2150250
  • 4,797
  • 11
  • 36
  • 44
  • `(?s)?:Here` wrong. Where is the opening paranthesis? Check the regex again. – Avinash Raj Nov 21 '14 at 17:08
  • Did you mean this http://regex101.com/r/uT5cC0/4 ? – Avinash Raj Nov 21 '14 at 17:15
  • Where you want a backslash character (\\) in a string literal to be interpreted by the pattern compiler, you must escape it to prevent it being parsed as a character escape: `"...\\?..."`. This is optional where the string parser and the pattern compiler will interpret the escape the same way, such as for the sequence `\n`. – John Bollinger Nov 21 '14 at 17:48
  • @JohnBollinger Ah of course, how could I forget. My mistake. Thanks! Does anyone want to put a response to this I can mark as an answer? – user2150250 Nov 21 '14 at 17:55

1 Answers1

1

Seems like you want something like this,

String idInfo = "Any text up here\n" +
        "Here is the id\n" +
        "\n" +
        "?a0 12 b5\n" +
        "&Edit Properties...\n" +
        "And any text down here";
Pattern regex = Pattern.compile("Here is the id\\n+\\?([a-z0-9]{2}(?:\\s[a-z0-9]{2})*)(?=\\n&Edit Properties)");
Matcher matcher = regex.matcher(idInfo);
while(matcher.find()){
        System.out.println(matcher.group(1));
}

Output:

a0 12 b5

DEMO

Avinash Raj
  • 172,303
  • 28
  • 230
  • 274