我将如何编写捕获 A23 但不捕获 A 23 的正则表达式 [英] How would I write a regular expression that captures A23 but not A 23

查看:48
本文介绍了我将如何编写捕获 A23 但不捕获 A 23 的正则表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将如何编写执行以下操作的正则表达式:

How would I write a regular expression that does the following:

Given:  A23+    returns   A23+
Given:  A 23+   returns   23+
Given:  A.rg23- returns   A.rg23-
Given:  A .rg23- returns  .rg23-

这是通过积极的前瞻来最好地完成还是 [^ ] 能够实现这一点?

Is this best accomplished via a positive lookahead or would the [^ ] be able to achieve this?

推荐答案

实际上,如果大写字母 A 后面有一个空格,我会忽略 A 和空格并捕获其他所有内容.但是如果 A 之后没有空格,我想在捕获中包含 A.

sically if there is a space after the capital A, I would to ignore the A and the space and capture everything else. But if there is no space after the A I want to include the A in the capture.

你可以在 PCRE 中使用 match reset \K 试试这个正则表达式:

You can try this regex in PCRE using match reset \K:

\bA(?: \K)?\S+

\K 重置报告匹配的起点.任何先前消耗的字符不再包含在最终匹配中.所以在这种情况下,如果 A 后面有一个空格,那么 \K 会重置匹配的信息,从而允许我们捕获它后面的任何内容,即 \S+.

\K resets the starting point of the reported match. Any previously consumed characters are no longer included in the final match. So in this case if there is a space after A then \K resets matched information thus allowing us to capture whatever comes after it i.e. \S+.

正则表达式演示

这篇关于我将如何编写捕获 A23 但不捕获 A 23 的正则表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆