1

I have the following input:

8=FIX.4.2|9=00394|35=8|49=FIRST|8=FIX.4.2|9=00394|35=8|56=MIDDLE|10=245|8=FIX.4.2|9=00394|35=8|49=LAST|56=HEMADTS|10=024|

Now I want the strings that are starting with "8=???" and end with "10=???|". You can see above that there are exactly two strings that start with 8 and end with 10. I have written a program for this.

Below is my code:

public class Main {
  static Pattern r = Pattern.compile("(.*?)(8=\\w\\w\\w)[\\s\\S]*?(10=\\w\\w\\w)");

  public static void main(String[] args) {     
       String str = "8=FIX.4.2|9=00394|35=8|49=FIRST|8=FIX.4.2|9=00394|35=8|56=MIDDLE|10=245|8=FIX.4.2|9=00394|35=8|49=LAST|56=HEMADTS|10=024|";
       match(str);
  }

  public static void match(String message) { //send to OMS

        Matcher m = r.matcher(message);
        while (m.find()) {
              System.out.println(m.group());
        }
   }
}

When I just run this I am getting the wrong output like:

8=FIX.4.2|9=00394|35=849=FIRST`|8=FIX.4.2|9=00394|35=8|56=MIDDLE|10=245|`
8=FIX.4.2|9=00394|35=849=LAST|56=HEMADTS|10=024|

You can see the first string in the output. It consists of "8=???" two times but the exact output needs to be like:

8=FIX.4.2|9=00394|35=8|56=MIDDLE|10=245|
8=FIX.4.2|9=00394|35=849=LAST|56=HEMADTS|10=024|

I also want the un-matched strings in separate as there is a further work with those strings. How can I get that? So, the total output needs to be like:

Matched : 8=FIX.4.2|9=00394|35=8|56=MIDDLE|10=245|
Matched : 8=FIX.4.2|9=00394|35=849=LAST|56=HEMADTS|10=024|
UnMatched : 8=FIX.4.2|9=00394|35=849=FIRST`|
8
  • I don't understand why the two currently matching strings don't meet your expectation. Commented Aug 18, 2017 at 13:21
  • Take the two currently matching strings. The first string has TWO "8=???" but the exact match is only ONE "8=???" to "10=???" Commented Aug 18, 2017 at 13:24
  • I see at least 4 strings that start with 8= and end with 10= in your input example. Commented Aug 18, 2017 at 13:26
  • Sorry, there are only two strings that exactly start with "8=FIX.4.2" and end with "10=XXX". Commented Aug 18, 2017 at 13:29
  • 8=FIX.4.2 appears 3 times, 10= appears twice. Do you mean a match that does not include another 8=FIX.4.2? Commented Aug 18, 2017 at 13:31

1 Answer 1

3

You need to use a tempered greedy token to match the shortest window possible between 2 strings. That will solve the first problem. To get unmatched strings, just split the string with the pattern.

Use

\b8=\w{3}(?:(?!8=\w{3})[\s\S])*?10=\w{3}\|

See the regex demo.

Details

  • \b - a word boundary
  • 8= - a literal substring
  • \w{3} - 3 word chars
  • (?:(?!8=\w{3})[\s\S])*? - a tempered greedy token matching any char ([\s\S]), zero or more times, as few as possible, that do not start a 8= and 3 word chars pattern
  • 10= - a literal substring
  • \w{3} - 3 word chars
  • \| - a literal |.

Java code:

public static Pattern r = Pattern.compile("\\b8=\\w{3}(?:(?!8=\\w{3})[\\s\\S])*?10=\\w{3}\\|");
public static void main (String[] args) throws java.lang.Exception
{
   String str = "8=FIX.4.2|9=00394|35=8|49=FIRST|8=FIX.4.2|9=00394|35=8|56=MIDDLE|10=245|8=FIX.4.2|9=00394|35=8|49=LAST|56=HEMADTS|10=024|";
   match(str);
}
public static void match(String message) { //send to OMS
    Matcher m = r.matcher(message);
    System.out.println("MATCHED:");
    while (m.find()) {
       System.out.println(m.group());
    }
    System.out.println("UNMATCHED:");
    String[] unm = r.split(message);
    for (String s: unm) {
        System.out.println(s);  
    }
}

See the Java demo.

Results:

MATCHED:
8=FIX.4.2|9=00394|35=8|56=MIDDLE|10=245|
8=FIX.4.2|9=00394|35=8|49=LAST|56=HEMADTS|10=024|
UNMATCHED:
8=FIX.4.2|9=00394|35=8|49=FIRST|
Sign up to request clarification or add additional context in comments.

1 Comment

I think I misunderstood the input string and/or spent no time actually testing in Java. +1 in any case.

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.