dsn1327
dsn1327
2017-04-23 07:32

如何在正则表达式中用空格和数字来拉?

已采纳

I have following couple of lines:

  • The Great Gatsby (1925) by F. Scott Fitzgerald is a short novel which takes.
  • Fight Club is a 1996 novel by Chuck Palahniuk.

I have to pick writer name that comes after by and year it was written; 1925 and 1996 respectively. What I figured, I need a couple of RegEx; one for picking numbers only by doing \d+ and other for picking writer name. For Writer I made this regex(https://regex101.com/r/Z55yz3/1) but it is not working as it should due to presence of . and space.

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

2条回答

  • duaj39673 duaj39673 4年前

    [0-9]{4} - takes 4-digits number

    \bby - gets word 'by' and

    (?:\s+[A-Z][\w.]+)+- selects following words and abbreviations, beginning with uppercase letter

    ([0-9]{4}).*(?:\bby)((?:\s+[A-Z][\w.]+)+)
    

    Demo and explanation

    点赞 评论 复制链接分享
  • dongsunny1113 dongsunny1113 4年前

    you can use this regex /^.+?\ BY (.+)$/is like this

    $string_text = 'The Great Gatsby (1925) by F. Scott Fitzgerald is a short novel which takes.';
    preg_match("/^.+?\ BY (.+)$/is" , $string_text, $match);
    echo $match[1];
    
    点赞 评论 复制链接分享

相关推荐