试图用正则表达式解析字符

时间:2022-03-13 22:58:16

I'm trying to use regex on an exchange server to match the rule "does the email address have a number in character 1,2 or 3"

我正在尝试在Exchange服务器上使用正则表达式来匹配规则“电子邮件地址是否具有字符1,2或3中的数字”

I've never used RegEx before, but i came up with the following, which ALMOST works!

我之前从未使用过RegEx,但是我想出了以下内容,ALMOST正常工作!

^([0-9]|[0-9]|[0-9]).*@domain.com

^([0-9] | [0-9] | [0-9])。* @ domain.com

It works if Char 2 or 3 are numbers, but not the first character

如果Char 2或3是数字,则它有效,但不是第一个字符

Can someone see / explain where i am going wrong?

有人能看到/解释我哪里出错吗?

Many thanks,

非常感谢,

J

Ĵ

2 个解决方案

#1


2  

Your regex is cheking if the first character is a digit ([0-9]), a digit([0-9]) or a digit([0-9]) : ([0-9]|[0-9]|[0-9])

如果第一个字符是数字([0-9]),数字([0-9])或数字([0-9]),你的正则表达式正在che :( [0-9] | [0-9 ] | [0-9])

You can do what you want whith this :

你可以做你想要的事情:

^([0-9]..|.[0-9].|..[0-9]).*@domain.com

This check if your string start with :

这检查你的字符串是否以:

[0-9].. a digit followed by two characters

[0-9] ..一个数字后跟两个字符

.[0-9]. a character, a digit and one more character

[0-9]。一个字符,一个数字和一个字符

..[0-9] two characters and a digit

.. [0-9]两个字符和一个数字

You can test it here

你可以在这里测试一下

#2


1  

You are going wrong by using alternation in your brackets when you are really trying to match characters at positions.

当您真正尝试匹配位置上的字符时,在括号中使用替换会出错。

If you want to match numbers at position 1, 2 OR 3, this would work:

如果你想匹配位置1,2或3的数字,这将有效:

^[0-9]|^.[0-9]|^..[0-9]

Note that this is not going to validate your string as an email address, but assuming you feed only valid email addresses to the regex it will match what you want it to match.

请注意,这不会将您的字符串验证为电子邮件地址,但假设您只向正则表达式提供有效的电子邮件地址,它将匹配您希望它匹配的内容。

#1


2  

Your regex is cheking if the first character is a digit ([0-9]), a digit([0-9]) or a digit([0-9]) : ([0-9]|[0-9]|[0-9])

如果第一个字符是数字([0-9]),数字([0-9])或数字([0-9]),你的正则表达式正在che :( [0-9] | [0-9 ] | [0-9])

You can do what you want whith this :

你可以做你想要的事情:

^([0-9]..|.[0-9].|..[0-9]).*@domain.com

This check if your string start with :

这检查你的字符串是否以:

[0-9].. a digit followed by two characters

[0-9] ..一个数字后跟两个字符

.[0-9]. a character, a digit and one more character

[0-9]。一个字符,一个数字和一个字符

..[0-9] two characters and a digit

.. [0-9]两个字符和一个数字

You can test it here

你可以在这里测试一下

#2


1  

You are going wrong by using alternation in your brackets when you are really trying to match characters at positions.

当您真正尝试匹配位置上的字符时,在括号中使用替换会出错。

If you want to match numbers at position 1, 2 OR 3, this would work:

如果你想匹配位置1,2或3的数字,这将有效:

^[0-9]|^.[0-9]|^..[0-9]

Note that this is not going to validate your string as an email address, but assuming you feed only valid email addresses to the regex it will match what you want it to match.

请注意,这不会将您的字符串验证为电子邮件地址,但假设您只向正则表达式提供有效的电子邮件地址,它将匹配您希望它匹配的内容。