-1

I need to extract the values after :70: in the following text file using RegEx. Value may contain line breaks as well.

My current solution is to extract the string between :70: and : but this always returns only one match, the whole text between the first :70: and last :.

:32B:xxx,
:59:yyy
something
:70:ACK1
ACK2
:21:something
:71A:something
:23E:something
value
:70:ACK2
ACK3
:71A:something

How can I achive this using Java? Ideally I want to iterate through all values, i.e.

ACK1\nACK2, ACK2\nACK3

Thanks :)

Edit: What I'm doing right now,

Pattern pattern = Pattern.compile("(?<=:70:)(.*)(?=\n)", Pattern.DOTALL);
Matcher matcher = pattern.matcher(data);
while (matcher.find()) {
   System.out.println(matcher.group())
}
5
  • @Jens added my current solution.
    – flopr
    Oct 21, 2016 at 7:01
  • do you want the 32B or only 32 from your input ? Oct 21, 2016 at 7:02
  • @PavneetSingh I only need ACK1\nACK2 and ACK2\nACK3. Don't care about anything else. ;)
    – flopr
    Oct 21, 2016 at 7:06
  • 1
    If you are sure that : is always and only used as separator, you could simply use (?<=:70:)([^:]*). This might catch a trailing newline, but you should be able to trim it off. Oct 21, 2016 at 7:16
  • @SebastianProske works! thanks :)
    – flopr
    Oct 21, 2016 at 7:39

2 Answers 2

3

Try this.

String data = ""
    + ":32B:xxx,\n"
    + ":59:yyy\n"
    + "something\n"
    + ":70:ACK1\n"
    + "ACK2\n"
    + ":21:something\n"
    + ":71A:something\n"
    + ":23E:something\n"
    + "value\n"
    + ":70:ACK2\n"
    + "ACK3\n"
    + ":71A:something\n";
Pattern pattern = Pattern.compile(":70:(.*?)\\s*:", Pattern.DOTALL);
Matcher matcher = pattern.matcher(data);
while (matcher.find())
    System.out.println("found="+ matcher.group(1));

result:

found=ACK1
ACK2
found=ACK2
ACK3
0
-1

You need a loop to do this.

Pattern p = Pattern.compile(regexPattern);
List<String> list = new ArrayList<String>();
Matcher m = p.matches(input);
while (m.find()) {
    list.add(m.group());
}

As seen here Create array of regex matches

5
  • I'm already doing this but matcher.find() only finds a single match. :( Please also check my current solution. I added it to the question.
    – flopr
    Oct 21, 2016 at 7:01
  • I don't see a loop in the solution provided.
    – kpie
    Oct 21, 2016 at 7:08
  • I have the while (matcher.find()) {} after creating the Matcher. I think the issue is more related to my RegEx because I only get one match.
    – flopr
    Oct 21, 2016 at 7:11
  • If your loop is empty than you aren't putting your results together. The list.add(m.group()) is important.
    – kpie
    Oct 21, 2016 at 7:12
  • No my loop is not empty; I'm using the value returned from m.group() -.- Updated the question to make this clear.
    – flopr
    Oct 21, 2016 at 7:16

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.