正则表达式Java不匹配

时间:2013-12-04 21:37:53

标签: java regex

我有以下文字:http://pastebin.com/xpaC4JXR

我想得到演员的名字,在这个例子中Will Smith,我在Java中使用以下代码:

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

this.name = mName.group(2);

响应是文本。

我尝试使用可视化的Regex for Java,它似乎有效:http://i.stack.imgur.com/y3hIv.png

但是当我执行时,我得到:

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 - &gt; this.nome = mNome.group(2);

3 个答案:

答案 0 :(得分:3)

没有查看你的正则表达式,但你需要调用这两种方法中的任何一种才能调用Matcher#group(2)

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

答案 1 :(得分:0)

两个问题。 @anubhava提到的一个......你需要在你的匹配器中调用一个方法来找到这些组。 “找到”会起作用。其次,你没有逃脱模式字符串中的一个引号。我打赌这是一个错字。

无论如何,这是示例代码:

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());
}

这是输出:

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

在此播放:http://ideone.com/wOEC15

答案 2 :(得分:-1)

您可以尝试使用\\代替\

相关问题