簡體   English   中英

正則表達式Java不匹配

[英]Regex Java doesn't match

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

我想獲得演員的名字,在這個例子中,威爾·史密斯,我在Java中使用以下代碼:

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

this.name = mName.group(2);

響應是文本。

我嘗試使用Java的可視Regex,它似乎可以正常工作: 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 > this.nome = mNome.group(2);

尚未查看您的正則表達式,但您需要先調用以下兩種方法之一,然后才能調用Matcher#group(2)

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

兩個問題。 @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

您可以嘗試使用\\\\代替\\

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM