简体   繁体   English

正则表达式向后匹配最接近的字符

[英]Regex to match the nearest character backwards

I have this string: 我有这个字符串:

P.1           P.2                    P.3                   P.4  
          ASTON VETERINARY HOSPITAL                                       
            Page 1/2   
        00 PennelJ Road  
      Media, PA 19063-5983 
          (610) 474-5670           
Client :      

I want to get the text in between Client and P.\\d . 我想在ClientP.\\d之间获取文本。 Here is the demo: Regex 这是演示:正则表达式

(P.\d)[\s\S]*(?=^.+Client :?)

The problem is that it matches from the first Page P.1 . 问题在于它与第一页P.1相匹配。 I need the nearest P.\\d before Client. 我需要在客户之前最接近的P.\\d

How to change the regex so that it would match from P.4 . 如何更改正则表达式,使其与P.4匹配。

I tried this with non-greedy operators but that's not going to work. 我尝试过使用非贪婪的运算符,但这是行不通的。 I'd try to move away from having the entire regex match precisely what you want, and use groups instead. 我会尝试使整个正则表达式与您想要的完全匹配,而改为使用组。 Then you can just write a matcher to match any number of those P.1 constructs, and it makes your scan for the Client string at the end a lot simpler because you don't have to try to do it as a lookahead. 然后,您可以编写一个匹配器以匹配任意数量的P.1构造,这样一来,您扫描Client字符串的末尾就简单得多了,因为您不必先行尝试。 Thus: 从而:

String x = "P.1  P.2    P.3   P.4 foobar  Client :";
Pattern p = Pattern.compile("((P\\.\\d)(.*(P\\.\\d))*)+(?<result>.*)Client");
Matcher m = p.matcher(x);
System.out.println(m.find());
System.out.println(m.group("result"));

Seems to produce precisely what you want. 似乎精确地产生了您想要的。 The syntax (?<whatever>REGEX HERE) is regular-expression-ese for: Let me grab just this bit later by asking for the group 'whatever'. 语法(?<whatever>REGEX HERE)是正则表达式,用于:让我稍后通过询问组“ whatever”来抓住这一点。

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

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