我将如何编写捕获 A23 但不捕获 A 23 的正则表达式

How would I write a regular expression that captures A23 but not A 23

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

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

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

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 中尝试此正则表达式 \K:

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

\K 重置报告匹配的起点。任何先前消耗的字符都不再包含在最终匹配中。因此,在这种情况下,如果 A 之后有 space,则 \K 会重置匹配信息,从而使我们能够捕获其后的任何内容,即 \S+.

RegEx Demo

这是另一种方法。

/(?:A )(\S+)|(A\S+)/g

在 regex101.com 上生成正确的匹配项。