[英]C# Regex - Match certain char followed by number/identifier
I'm in trouble with a Regex
which seems to have never been asked here.我遇到了一个似乎从未在这里被问过的正则
Regex
。 I have to replace the char a
followed by a whitespace
(or not followed), but necessarly followed by a number
(the number must not be replaced).我必须替换 char
a
后跟一个whitespace
(或不跟随),但必须后跟一个number
(不能替换该数字)。
I have this Regex: [aA]\s.(?<=\d)*
and this is the result:我有这个正则表达式:
[aA]\s.(?<=\d)*
这就是结果:
using (?<=\d)*
I wanted to try to match but not capture the number immediately after the character following (or not) from the space, but obviously it doesn't work, also because "\d" does not include the identifiers.使用
(?<=\d)*
我想尝试匹配但不立即捕获空格后面(或不)后面的字符之后的数字,但显然它不起作用,也是因为 "\d" 不包括标识符。 Identifiers can be a series of numeric or alphanumeric characters without a defined length, nor a sorting of the letters in case it was alphanumeric.标识符可以是一系列没有定义长度的数字或字母数字字符,也可以是字母排序(如果它是字母数字)。 They can be
A54N3
, Z4G78
or 8454
or 4AZ7
or 7
or A1
, 1A
.它们可以是
A54N3
、 Z4G78
或8454
或4AZ7
或7
或A1
、 1A
。 Combinations always change.组合总是在变化。
I'd want to match ONLY the a
before the number 8
(or any other number, or an identifier like N574A
) and replace that char with art
, but leaving the number /identifier as it is, so result should be: agricoltura n 6 sensi dell'art8
or agricoltura n 6 sensi dell'artN574A
, and if the target string was agricoltura n 6 sensi dell'a8
or agricoltura n 6 sensi dell'aN574A
, (so without whitespace) result should be: agricoltura n 6 sensi dell'art8
or agricoltura n 6 sensi dell'artN574A
我只想匹配数字
8
(或任何其他数字,或像N574A
类的标识符)之前的a
并将该 char 替换为art
,但保持数字 /identifier 不变,因此结果应该是: agricoltura n 6 sensi dell'art8
或agricoltura n 6 sensi dell'artN574A
,如果目标字符串是agricoltura n 6 sensi dell'a8
或agricoltura n 6 sensi dell'aN574A
,(所以没有空格)结果应该是: agricoltura n 6 sensi dell'art8
或 agricoltura agricoltura n 6 sensi dell'artN574A
So the generic rule should be: Match [aA] followed by an optional space then must be followed by a number or an identifier that must not be captured
所以通用规则应该是:
Match [aA] followed by an optional space then must be followed by a number or an identifier that must not be captured
Is it possible to do such a thing?有可能做这样的事情吗? What could be the solution?
有什么解决办法? Thank you so much!
太感谢了!
Using the \\b([aA])\\s*([A-Za-z]*\\d[\\dA-Za-z]*)\\b
pattern seems to replace correct values, here is the demo使用
\\b([aA])\\s*([A-Za-z]*\\d[\\dA-Za-z]*)\\b
模式似乎替换了正确的值, 这里是演示
You may use您可以使用
\b([aA])\s*([A-Za-z]*\d[\dA-Za-z]*)\b
Replace with $1rt$2
.替换为
$1rt$2
。 See the regex demo查看正则表达式演示
Details细节
\b
- a word boundary \b
- 单词边界([aA])
- Group 1 (referred to with $1
from the replacement pattern): a
or A
([aA])
- 第 1 组(用替换模式中的$1
表示): a
或A
\s*
- 0 or more whitespaces \s*
- 0 个或更多空格([A-Za-z]*\d[\dA-Za-z]*)
- Group 2 (referred to with $2
from the replacement pattern): an alphanumeric whole word that contains at least one digit: ([A-Za-z]*\d[\dA-Za-z]*)
- 第 2 组(在替换模式中使用$2
表示):包含至少一个数字的字母数字完整单词:
[A-Za-z]*
- zero or more ASCII letters [A-Za-z]*
- 零个或多个 ASCII 字母\d
- a digit \d
- 一个数字[\dA-Za-z]*
- 0+ digits or ASCII letters (replace \d
with 0-9
to match ASCII digits only, or pass RegexOptions.ECMAScript
flag to Regex
constructor) [\dA-Za-z]*
- 0+ 个数字或 ASCII 字母(将\d
替换为0-9
以仅匹配 ASCII 数字,或将RegexOptions.ECMAScript
标志传递给Regex
构造函数)\b
- word boundary. \b
- 单词边界。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.