简体   繁体   English

正则表达式查找名字基于

[英]Regex Find firstname based on

Need some help with the firstname match based on search "Name" below the first line.根据第一行下方的搜索“名称”,需要一些有关名字匹配的帮助。 Below 3 situations that can occur:可能出现以下3种情况:

Mrs. Joanne Doe
Name detail

Jenni Doe
Name detail

Mr. John Doe
Name detail

This will solve the 2nd situation (^\w+)(?=.*\nName) .这将解决第二种情况(^\w+)(?=.*\nName) I need 1 regex that will cope with all the 3 scenario's.我需要 1 个正则表达式来处理所有 3 个场景。

Thanks!谢谢!

SOLVED By Wiktor with:由 Wiktor 解决:

^(?:M(?:rs?|(?:is)?s)\.?\s*)?\K\w+(?=.*\nName)

Demo演示

You can use您可以使用

^(?:M(?:rs?|(?:is)?s)\.?\s*)?\K\w+(?=.*\RName)

See the regex demo .请参阅正则表达式演示 Details :详情

  • ^ - start of string ^ - 字符串的开头
  • (?:M(?:rs?|(?:is)?s)\.?\s*)? - an optional occurrence of Mr , Mrs , Miss or Ms optionally followed with a . - MrMrsMissMs的可选出现,可选后跟 a . char and then zero or more whitespaces char 然后是零个或多个空格
  • \K - omit all text matched so far \K - 忽略到目前为止匹配的所有文本
  • \w+ - one or more letters, digits or underscores \w+ - 一个或多个字母、数字或下划线
  • (?=.*\RName) - a positive lookahead that requires any zero or more chars other than line break chars as many as possible, a line break sequence and then Name string immediately to the right of the current position. (?=.*\RName) - 一个正向前瞻,它需要尽可能多的除换行符以外的任何零个或多个字符,一个换行符序列,然后是当前 position 右侧的Name字符串。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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