【问题标题】:Regex: match between the fisrt and last occurrence of a specific character正则表达式:匹配特定字符的第一次和最后一次出现
【发布时间】:2022-08-18 23:40:37
【问题描述】:

我有以下字符串:

FIn 2021 did you contribute any money to the plan with USPS for example through payroll deductionsF ZsZ LExclude rollovers or cashouts from other retirement accounts or pension plans as new contributionsL

我想从两个 \"F\" 之间的这个字符串中提取问题,得到一个干净的结果,例如:

In 2021 did you contribute any money to the plan with USPS for example through payroll deductions

我尝试了多种正则表达式,包括:

(?<=/)[^/\'f\']+(?=_[^\'f\']*$)

这没有产生我想要的回应。

非常感谢您提前提供的任何提示!

    标签: regex


    【解决方案1】:

    您可以使用

    (?<=\bF)[\w\W]*?(?=F\b)
    

    请参阅regex demo

    细节

    • (?&lt;=\bF) - 一个正向的后向查找,它匹配紧接在 F 之前的位置,该位置要么在字符串的开头,要么以非单词 char 开头
    • [\w\W]*? - 尽可能少的任何零个或多个字符
    • (?=F\b) - 一个正向的前瞻,需要一个 F 紧跟当前位置右侧的字符串结尾或非单词字符。

    【讨论】:

      猜你喜欢
      • 2011-01-02
      • 2022-11-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-18
      • 2012-11-17
      相关资源
      最近更新 更多