正则表达式无法按预期工作

时间:2022-09-18 21:44:43

How can it be that this regular expression also returns strings that have a _ underscore as their last character?

怎么可能这个正则表达式还返回有_下划线作为最后一个字符的字符串?

It should only return strings with alphabetical characters, mixed lower- and uppercase.

它应该只返回带有字母字符的字符串,混合使用大写和小写字母。

However, the regular expression returns: 'action_'

但是,正则表达式返回:'action_'

 $regEx = '/^([a-zA-Z])[a-zA-Z]*[\S]$|^([a-zA-Z])*[\S]$|^[a-zA-Z]*[\S]$/';

4 个解决方案

#1


The [\S] will match everything that is not whitespace, including underscore.

[\ S]将匹配所有不是空格的内容,包括下划线。


Also, your expression is very odd!

而且,你的表情很奇怪!

If you want a string that only contains letters, then use ^[a-zA-Z]*$ or ^[a-zA-Z]+$ (depending on if blank is allowed or not).

如果你想要一个只包含字母的字符串,那么使用^ [a-zA-Z] * $或^ [a-zA-Z] + $(取决于是否允许空白)。

If you're trying to do something else, you will need to expand on what that is.

如果你正在尝试做其他事情,你需要扩展它的内容。

#2


Because \S means "not whitespace character", \S matches _

因为\ S表示“不是空白字符”,\ S匹配_

A group should not have an underscore though, so, if you meant that, it could be that you are getting the whole match back and not just the first group.

一个小组不应该有一个下划线,所以,如果你的意思是,你可能会得到整个比赛而不仅仅是第一组。

Please show how are you using the regex to clarify that, if needed.

如果需要,请说明如何使用正则表达式来澄清这一点。

#3


\S matches any non-whitespace char - thus _

\ S匹配任何非空白字符 - 因此_

#4


You should show the text and what part from you want to extract from it. Regular expression shouldn't be so big like yours.

您应该显示文本以及要从中提取的部分。正则表达式不应该像你的那样大。

Work on small expression batches... At this size, is very difficult to help you.

处理小型表达批次...在这个大小,很难帮助你。

#1


The [\S] will match everything that is not whitespace, including underscore.

[\ S]将匹配所有不是空格的内容,包括下划线。


Also, your expression is very odd!

而且,你的表情很奇怪!

If you want a string that only contains letters, then use ^[a-zA-Z]*$ or ^[a-zA-Z]+$ (depending on if blank is allowed or not).

如果你想要一个只包含字母的字符串,那么使用^ [a-zA-Z] * $或^ [a-zA-Z] + $(取决于是否允许空白)。

If you're trying to do something else, you will need to expand on what that is.

如果你正在尝试做其他事情,你需要扩展它的内容。

#2


Because \S means "not whitespace character", \S matches _

因为\ S表示“不是空白字符”,\ S匹配_

A group should not have an underscore though, so, if you meant that, it could be that you are getting the whole match back and not just the first group.

一个小组不应该有一个下划线,所以,如果你的意思是,你可能会得到整个比赛而不仅仅是第一组。

Please show how are you using the regex to clarify that, if needed.

如果需要,请说明如何使用正则表达式来澄清这一点。

#3


\S matches any non-whitespace char - thus _

\ S匹配任何非空白字符 - 因此_

#4


You should show the text and what part from you want to extract from it. Regular expression shouldn't be so big like yours.

您应该显示文本以及要从中提取的部分。正则表达式不应该像你的那样大。

Work on small expression batches... At this size, is very difficult to help you.

处理小型表达批次...在这个大小,很难帮助你。