JChris
JChris

Reputation: 1678

Regex Java doesn't match

I have the following text: http://pastebin.com/xpaC4JXR

And I want to get the name of the actor, in this example Will Smith, I'm using the following code in Java:

Matcher mName = Pattern.compile("(,\"name\":\")(.*)(\",\"place_of_birth")").matcher(response);

this.name = mName.group(2);

Where response is the text.

I tried with a visual Regex for Java and it seems to work: https://i.sstatic.net/y3hIv.png

But when I execute I get:

Exception in thread "main" java.lang.IllegalStateException: No match found
at java.util.regex.Matcher.group(Matcher.java:485)
at cinema.Ator.<init>(Ator.java:22)

Ator.java:22 --> this.nome = mNome.group(2);

Upvotes: 1

Views: 259

Answers (3)

sdanzig
sdanzig

Reputation: 4500

Two problems. One that @anubhava mentioned... you need to call a method in your matcher to find the groups. "find" will work. Secondly, you didn't escape one of the quotes in your pattern string. I'm betting that was a typo.

Anyway, here's the sample code:

String testString = "{\"adult\":false,\"also_known_as\":[\"The Fresh Prince\",\"DJ Jazzy Jeff and The Fresh Prince\",\"Fresh Prince\",\"Wil Smith\",\"Wiru Sumisu\"],\"biography\":\"From Wikipedia, the free encyclopedia\\n\\nWillard Christopher \\\"Will\\\" Smith, Jr. (born September 25, 1968) is an American actor, film producer and pop rapper. He has enjoyed success in music, television and film. In April 2007, Newsweek called him the most powerful actor on the planet. Smith has been nominated for four Golden Globe Awards, two Academy Awards, and has won multiple Grammy Awards.\\n\\nIn the late 1980s, Smith achieved modest fame as a rapper under the name The Fresh Prince. In 1990, his popularity increased dramatically when he starred in the popular television series The Fresh Prince of Bel-Air. The show ran for nearly six years (1990\u20131996) on NBC and has been syndicated consistently on various networks since then. In the mid-1990s, Smith transitioned from television to film, and ultimately starred in numerous blockbuster films that received broad box office success. In fact, he is the only actor in history to have eight consecutive films gross over $100 million in the domestic box office as well as being the only actor to have eight consecutive films in which he starred open at the #1 spot in the domestic box office tally.\\n\\nFourteen of the 19 fiction films he has acted in have accumulated worldwide gross earnings of over $100 million, and 4 of them took in over $500 million in global box office receipts. His most financially successful films have been Bad Boys, Bad Boys II, Independence Day, Men in Black, Men in Black II, I, Robot, The Pursuit of Happyness, I Am Legend, Hancock, Wild Wild West, Enemy of the State, Shark Tale, Hitch, and Seven Pounds. He also earned critical praise for his performances in Six Degrees of Separation, Ali, and The Pursuit of Happyness, receiving Best Actor Oscar nominations for the latter two.\\n\\nDescription above from the Wikipedia article Will Smith, licensed under CC-BY-SA, full list of contributors on Wikipedia.\",\"birthday\":\"1968-09-25\",\"deathday\":\"\",\"homepage\":\"http://www.willsmith.com/\",\"id\":2888,\"imdb_id\":\"nm0000226\",\"name\":\"Will Smith\",\"place_of_birth\":\"Philadelphia, Pennsylvania, USA\",\"popularity\":7.0175,\"profile_path\":\"/yTMJPPVHZ6P2zXQOg2pMRDBbfEf.jpg\"}";
Pattern pattern = Pattern.compile("(,\"name\":\")(.*)(\",\"place_of_birth\")");
Matcher matcher = pattern.matcher(testString);
while (matcher.find())
{
    System.out.print("Start index: " + matcher.start());
    System.out.print(" End index: " + matcher.end() + " ");
    System.out.println(matcher.group());
}

And here's the output:

Start index: 2147 End index: 2184 ,"name":"Will Smith","place_of_birth"

Play with it here: http://ideone.com/wOEC15

Upvotes: 0

Siva Atta
Siva Atta

Reputation: 91

you can try using \\ instead of \

Upvotes: -1

anubhava
anubhava

Reputation: 785146

Haven't looked at your regex but you need to call either of these 2 methods before you can call Matcher#group(2):

  1. Matcher#find()
  2. Matcher#matches()

Upvotes: 3

Related Questions