0

Apologies in advance if i am misunderstanding the use of Regex in this context.

I would like to retrieve a repeated field from a String. The string in question looks something like this -

TrendsJSONImpl{asOf=Fri Mar 08 00:04:26 GMT 2013, trendAt=Fri Mar 08 00:04:26 GMT 2013, >trends=[TrendJSONImpl{name='#TheBiggestLies', url='URL', query='%23TheBiggestLies'}, TrendJSONImpl{name='#ICanHonestlySay', >url='URL', query='%23ICanHonestlySay'}, >TrendJSONImpl{name='#EuTenhoUmaQuedaPor', url='URL', query='%23EuTenhoUmaQuedaPor'}, >TrendJSONImpl{name='#CitePessoasExclusivamenteSuas', url='URL', query='%23CitePessoasExclusivamenteSuas'},

From this string, I would like to retrieve the field "name" and add it to a list. This string represents the trending topics on Twitter and is subject to change every time the method that generates it is invoked.

The ideal output would be something like -

#TheBiggestLies

#ICanHonestlySay

#CitePessoasExclusivamenteSuas

Following previous articles on here I have attempted to extract the name field with the following code -

UI.model = new DefaultListModel();
            String trendsInfo = //FUNCTIONWHICHRETRIEVESSTRING
                    Matcher m = Pattern.compile("{name=").matcher(trendsInfo);
            Pattern p = Pattern.compile(
                    "{name='(.*),",
                    Pattern.DOTALL);
            Matcher matcher = p.matcher(trendsInfo);


            while (matcher.find()) {
                for (int i = 0; i < 20; i++) {
                    String output = m.group(i);
                    UI.model.addElement(output);
                    System.out.println(m.group(i));
                }
            }

This is unfortunately returning an Illegal Repetition exception and I am not sure how to handle multiple queries of the same field. Any help in this matter would be appreciated.

Thanks for your time!

iainmac
  • 23
  • 1
  • 4
  • An extra tip: Instead of `"{name='(.*),"` use `"{name='(.*?)'"` so the matching will end at the first apostrophe after the name. – Philippe A Mar 08 '13 at 00:40

2 Answers2

0

{ is a repetition operator for regex, so you need to escape it like so: \\{.

Philippe A
  • 176
  • 5
0

The reason you are getting the exception is that you have the character { in your regexp. It's a reserved character so has to be escaped (\\{).

Other than that, you seem to be missing the second ' (before the comma). Using a lazy regexp might also be a good idea, so the final version would be something like this: \\{name='(.*?)',. Maybe you even want to add the hash character to make it more accurate: \\{name='(#.*?)',...

mkataja
  • 970
  • 2
  • 10
  • 28
  • Hi there! Thanks very much for the swift reply. I've taken your suggestion on board and have changed the code, unfortunately I am still getting an exception, specifically -Exception in thread "AWT-EventQueue-0" java.util.regex.PatternSyntaxException: Illegal repetition {name= Any thoughts? Thanks! – iainmac Mar 08 '13 at 11:41
  • Did you also escape the other regexp on the third row of you example code? Always when you have a literal `{` you need to write `\\{` as `{` is reserved for repetitions. – mkataja Mar 08 '13 at 16:27
  • Hi mkataja, THanks very much for your help again! I have solved the exception issue, but for some reason it is not returning all the information from the string, only 5 results. Here is my most recent code - String trendsInfo = WorldWideTrendsList.toString(); System.out.println(trendsInfo); Pattern p = Pattern.compile( "(#.*?)\\'", Pattern.DOTALL); Matcher matcher = p.matcher(trendsInfo); while (matcher.find()) { String output = matcher.group(1); System.out.println(output); UI.model.addElement(output); – iainmac Mar 08 '13 at 16:40
  • Hi, looking at the code in your comment, I can't really tell what the problem might be. As this seems to be a slightly different issue than before, maybe you should post it as a new question? – mkataja Mar 08 '13 at 16:47
  • Hi mkataja! Thanks for the help I will do! – iainmac Mar 08 '13 at 17:13