1

I need to extract a substring that appears after a certain pattern in the input string. I have been trying various combinations but not getting expected output. The input string can be in following 2 forms

1. 88,TRN:2014091900217161 SNDR REF:149IF1007JMO2507 BISCAYNE BLVD STE
2. 88,TRN:2014091900217161 SNDR REF:149IF1007JMO2507

I need to write a regex that will be applicable to above 2 variations and extract '149IF1007JMO2507' part that follows 'SNDR REF:'. Please find below sample program that i have written.

import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class RegexTester {
        private static final String input = "88,TRN:2014091900217161 SNDR REF:149IF1007JMO2507 BISCAYNE BLVD STE";
        private static Pattern pattern = Pattern.compile(".*SNDR REF:(.*?)(\\s.)*");
        private static Matcher matcher = pattern.matcher(input);
        public static void main (String[] args) {
                if (matcher.matches()) {
                        System.out.println(matcher.group(1));
                }
        }
}

Output:149IF1007JMO2507 BISCAYNE BLVD STE

I want output to be '149IF1007JMO2507'

Thank you.

1
  • If it doesn't have to be regex i would use : private static String returnRef( String str ){ return str.substring( str.indexOf( "REF" ) + 4, str.indexOf( "REF" ) + 20 ); } Commented Apr 3, 2015 at 12:30

3 Answers 3

1

You can use the following idiom to find your sub-string:

String[] examples = {
    "88,TRN:2014091900217161 SNDR REF:149IF1007JMO2507 BISCAYNE BLVD STE",
    "88,TRN:2014091900217161 SNDR REF:149IF1007JMO2507"      
};
//                           ┌ look-behind for "SNDR REF:"
//                           |               ┌ anything, reluctantly quantified
//                           |               |   ┌ lookahead for 
//                           |               |   | whitespace or end of input
Pattern p = Pattern.compile("(?<=SNDR\\sREF:).+?(?=\\s|$)");
// iterating examples
for (String s: examples) {
    Matcher m = p.matcher(s);
    // iterating single matches (one per example here)
    while (m.find()) {
        System.out.printf("Found: %s%n", m.group());
    }
}

Output

Found: 149IF1007JMO2507
Found: 149IF1007JMO2507

Note

I expect you don't know in advance it's going to be "149IF1007JMO2507", hence the contextual matching.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot Mena. That's the output i wanted.
1

You can use this regexp:

private static Pattern pattern = Pattern.compile(".*SNDR REF:([^\\s]+).*");

This will take everything after "SNDR REF

Comments

1

You can do it with replaceAll

    str = str.replaceAll(".*(REF:(\\S+)).*", "$2");

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.