简体   繁体   中英

How to match any char in regex with a limited number of words in PHP

I need a regex that can match such a thing. My text has book names with their author name, I need a regex that can match the book name and the author name in most possible ways that the book name and the author name is written. for example the book name and the author name can be written like:

Non-stop India: Mark Tully
Non-stop India by Mark Tully
Non-stop India: Dr,Mark Tully
Non-stop India written by Mark Tully
Mark Tully has written a book called Non-stop India

the thing I was thinking about was that I can match

$bookname(.*?)$authorname

but the problem with this is that book name is at the beginning of the paragraph and the author name is at the end of it, it'll match the whole paragraph, so if there's a way I can limit the number of the words (like spaces) to 3 or 4 it'll be enough for me.

I think that counting the number of spaces in order to prevent capturing the paragraph is overkill. Instead we should focus on fine tune the regexp so that it does not jump across lines.

I'm not sure how this could possible fail if you are using a lazy *

See your original regexp: https://regex101.com/r/4EV0eg/1/

One way to (wrongly) capture the whole paragraph would be to use the 's' modifier and use a greedy * , like this: https://regex101.com/r/CcbSHY/1/

But that does not seem to be the regexp you are using.

You may try to inline the flags with (?mi-s)

See: https://regex101.com/r/wwHm5y/2/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM