0

I have a String

InsertedTextAry NO AGREEMENT EXISTS FOR AGENCY - ABCD< 102354 This is test<

Now the requirement is to see if the text:

NO AGREEMENT EXISTS FOR AGENCY

... exists in the above text or not and if it does then extract the text:

NO AGREEMENT EXISTS FOR AGENCY - XXXX

The part after NO AGREEMENT EXISTS FOR AGENCY - and before < can be of any length consisting of numbers and/or alphabets.

I have done the search with the code

protected void testMethod(String text){
    if(Pattern.matches(APPEND_FIRST + "NO AGREEMENT EXISTS FOR AGENCY" + APPEND_LAST, text)){
        // do something
    }
}

Now I need some help on how to extract the pattern NO AGREEMENT EXISTS FOR AGENCY - XXXX (where XXXX - can be of any length alphanumeric followed by <).

Joetjah
  • 6,292
  • 8
  • 55
  • 90
  • You should tokenize with the string.split() and place the results into an array perhaps...String[] result = str.split(" "); – Josh Engelsma Dec 02 '13 at 16:04
  • http://stackoverflow.com/questions/3472663/replace-all-occurences-of-a-string-using-stringbuilder This will help. – Joetjah Dec 02 '13 at 16:05
  • The possible reason for you not to get much response here, is because you don't show what you have tried to accomplish this. Even if it's faulty code, please post that so we can comment on what went wrong and how to improve, rather than writing all the code for you. – Joetjah Dec 02 '13 at 16:10
  • That will be very trouble some and costly because the string can be too large. – user3041300 Dec 02 '13 at 16:10
  • the above code is working fine for the search part. But I need help with the text extraction. – user3041300 Dec 02 '13 at 16:12

4 Answers4

1
public static void main(String[] args) {
    final String TEST = "InsertedTextAry NO AGREEMENT EXISTS FOR AGENCY - ABCD< 102354 This is test<";
      // Create a Pattern object
    Pattern r = Pattern.compile("NO AGREEMENT EXISTS FOR AGENCY[^<]*");
    if(TEST.contains("NO AGREEMENT EXISTS FOR AGENCY"))
    {
          // Now create matcher object.
          Matcher m = r.matcher(TEST);
          if (m.find( )) {
             System.out.println("Found value: " + m.group(0) );
    }
}

Output:

Found value: NO AGREEMENT EXISTS FOR AGENCY - ABCD
Harry
  • 1,362
  • 12
  • 19
0

Try this

String text = "InsertedTextAry NO AGREEMENT EXISTS FOR AGENCY - ABCD< 102354 This is test<";
String pattern1 = "NO AGREEMENT EXISTS FOR AGENCY";
String pattern2 = "<";
if (text.indexOf(pattern1) > -1){
    String value = text.substring(text.indexOf(pattern1));
    value = value.substring(0, value.indexOf(pattern2));
    System.out.println(value);
}
Joetjah
  • 6,292
  • 8
  • 55
  • 90
Prashant Thakkar
  • 1,383
  • 1
  • 12
  • 15
0

This sholud work:

public static void main(String[] args) {
    String str = "InsertedTextAry NO AGREEMENT EXISTS FOR AGENCY - ABCD< 102354 This is test<";
    Pattern pattern = Pattern.compile("^.*(NO AGREEMENT EXISTS FOR AGENCY - .*)< (.*)$");
    Matcher m = pattern.matcher(str);
    if (m.find()) {
        System.out.println(m.group(1));
    }
}

this code prints:

NO AGREEMENT EXISTS FOR AGENCY - ABCD

And by using regexp with groups you can extract any part of the message you want

bary
  • 1,699
  • 2
  • 15
  • 24
0

You can do the following:

String text = "InsertedTextAry NO AGREEMENT EXISTS FOR AGENCY - ABCD< 102354 This is test<";
    String[] arr = text.split("<")";
    if (arr != null && arr.length > 0) {
         String answer = arr[0].substring(arr[0].indexOf("NO AGREEMENT EXISTS FOR AGENCY"));
    }

answer: "NO AGREEMENT EXISTS FOR AGENCY - ABCD"

Lital Kolog
  • 1,301
  • 14
  • 39