PHP棘手的正则表达式获取带引号的字符串直到某些单词

 收藏

我有一个看起来像这些字符串中的任何一个的变体

First rounder 'John Smith' had a good game.

Second rounder 'Jim O'Rielly' is on fire!

What I ultimately want is to get both names between quotes John Smith and Jim O'Rielly, however the tricky part is the names that include apostrophe like the second.

I initially was using '/\'([^\']*)\'/' to get the text inside the quotes, but doesn't work for the second case - this would only return Jim O.

I then thought to use .+?(?=had) in order to get everything up to the word had, but it needs to be either had or is, and I don't want the words First rounder, etc.

I need to essentially combine these, so I can get only the text inside the quotes, but UP UNTIL either word had or is, and I just want the text without quotes.

除非有一个技巧可以使第二个选项忽略名称中的撇号(我本以为addSlashes(),但我怎么知道要在其中添加斜杠的撇号?),没有人可以建议对此采取更好的解决方案吗?可以在名称中找到忽略我没有考虑的特殊字符的加分点:)

回复
  • You can alternate between matching non-'s, and matching 's which have word characters on either side. This way 's in the middle of a word will be matched, but 's at either end of a word won't.

    '((?:[^']+|\b'\b)+)'
    

    https://regex101.com/r/L9Em5l/1