0

I have a String which contains the following sub string:

[Qual:3] [Text:PIX 1252471471953/YHYF/PPP121.40/10RTY10/NOLXX08X1] [Elem:123]

I'd like to extract the part between [Text: and ] i.e. PIX 1252471471953/YHYF/PPP121.40/10RTY10/NOLXX08X1.

How do I do this?

mip
  • 1,886
  • 8
  • 26
  • 32

4 Answers4

4
Pattern p = Pattern.compile("\\[Text:(.*?)\\]");
Matcher m = p.matcher("[Qual:3] [Text:PIX 1252471471953/YHYF/PPP121.40/10RTY10/NOLXX08X1] [Elem:123]");
m.find();
System.out.println(m.group(1));

Gives:

PIX 1252471471953/YHYF/PPP121.40/10RTY10/NOLXX08X1

The \\[ and \\] are to escape the brackets, which are special characters in regexes. The .*? is a non-greedy quantifier, so it stops gobbling up characters when it reaches the closing bracket. This part of the regex is given inside a capturing group (), which you can access with m.group(1).

beerbajay
  • 19,652
  • 6
  • 58
  • 75
2

Use the following string as the regex:

"\\[Text:(.*?)\\]"

The first capture group will give you exactly the substring you want.

The non-greedy match (.*?) is required to make it stop at the first ] rather than also including [Elem:123].

NPE
  • 486,780
  • 108
  • 951
  • 1,012
  • Beat me to it! +1. @mip, note the difference between `.*?` and `.*`. One of the commenters suggested `.*`, but that doesn't work; it doesn't stop where it should. – Alex D Feb 21 '12 at 15:11
0
String.substring(int beginIndex, int endIndex)

Returns a new string that is a substring of this string.

You could use this to remove the start and end of the string,

or....

You could use

String.indexOf(String str) 

To get the index of the start and end of the match and copy the contents to a new result string.

You could use

String.matches(String regex) 

However writing regular expressions can get difficult,

http://docs.oracle.com/javase/6/docs/api/java/lang/String.html

I hope this helps.

garyamorris
  • 2,587
  • 2
  • 17
  • 23
0

Instead of using "\\[Text:(.*?)\\]", as others have suggested, I'd go one step further and use lookarounds to filter out the text you don't want:

(?<=\\[Text:).*?(?=\\])

This will match exactly the text you want without having to select a capturing group.

GenericJon
  • 8,746
  • 4
  • 39
  • 50
  • thanks for that. What would the Java look like? Would I replace `m.find` and `m.group(1)`? – mip Feb 21 '12 at 16:17