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 -

2 Answers
2

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='(#.*?)',...

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!
–
iainmacMar 8 '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.
–
mkatajaMar 8 '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);
–
iainmacMar 8 '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?
–
mkatajaMar 8 '13 at 16:47

Hi mkataja! Thanks for the help I will do!
–
iainmacMar 8 '13 at 17:13