I need a RegEx to match an uppercase string ending with a colon. The string can contain spaces, numbers and periods. So that if:
我需要一个RegEx来匹配以冒号结尾的大写字符串。字符串可以包含空格、数字和句点。所以,如果:
mystring = "I have a C. GRAY CAT2:"
I want the coldfusion expression
我想要coldfusion表达式。
REFind("[A-Z0-9. ][:]",mystring)
to return the number 9, matching "C. GRAY CAT2:". Instead, it is returning the number 21, matching only the colon. I hope that a correction of the regex will solve the problem. Of course I have tried many, many things. Thank you!
返回数字9,匹配“C. GRAY CAT2:”。相反,它返回的是第21号,只匹配冒号。我希望regex的修正能够解决这个问题。当然,我尝试过很多很多东西。谢谢你!
2 个解决方案
#1
2
I suggest using
我建议使用
[A-Z0-9][A-Z0-9. ]*:
See the regex demo
看到regex演示
Details
细节
-
[A-Z0-9]
- an uppercase letter or digit (in case the first char can be a digit, else remove0-9
) - [a - z0 -9] -大写字母或数字(如果第一个字符可以是数字,则删除0-9)
-
[A-Z0-9. ]*
- zero or more uppercase letters/digits,.
or space - [A-Z0-9。- 0或更多的大写字母/数字。或空间
-
:
- a colon. - :-一个冒号。
Variations
变化
To avoid matching 345:
like substrings but still allow 23 VAL:
like ones, use
要避免匹配345:就像子字符串一样,但仍然允许23 VAL: like ones, use。
\b(?=[0-9. ]*[A-Z])[A-Z0-9][A-Z0-9. ]*:
See this regex demo. Here, \b(?=[0-9. ]*[A-Z])
matches a word boundary first, and then the positive lookahead (?=[0-9. ]*[A-Z])
makes sure there is an uppercase letter after 0+ digits, spaces or dots.
看到这个正则表达式演示。在这里,\ b(? =[0 - 9。[a - z])首先匹配一个词的边界,然后是正的前视(?=[0-9]。[A-Z])确保在0+数字、空格或点之后有一个大写字母。
If you do not expect numbers at the start of the sequence, i.e. out of I have a 22 C. GRAY CAT2:
, you need to extract C. GRAY CAT2
, use Sebastian's suggestion (demo).
如果你不希望数字出现在序列的开始,例如我有一个22 C. GRAY CAT2:,你需要提取C. GRAY CAT2,使用Sebastian的建议(demo)。
#2
0
Have revised the selected answer to my own question to cover the German special characters.
将选定的答案修改为我自己的问题,以覆盖德国的特殊字符。
[A-Z][A-Z0-9.ÜÄÖß ]*:
This appears to work, however the Germans have recently added a capital ß to their alphabet, which is surely not on most keyboards yet, and therefore will not be a problem for the RegEx for a while.
这似乎工作,然而德国最近增加了一个资本ß字母,这肯定不是大多数键盘,因此将不会是一个问题的正则表达式。
#1
2
I suggest using
我建议使用
[A-Z0-9][A-Z0-9. ]*:
See the regex demo
看到regex演示
Details
细节
-
[A-Z0-9]
- an uppercase letter or digit (in case the first char can be a digit, else remove0-9
) - [a - z0 -9] -大写字母或数字(如果第一个字符可以是数字,则删除0-9)
-
[A-Z0-9. ]*
- zero or more uppercase letters/digits,.
or space - [A-Z0-9。- 0或更多的大写字母/数字。或空间
-
:
- a colon. - :-一个冒号。
Variations
变化
To avoid matching 345:
like substrings but still allow 23 VAL:
like ones, use
要避免匹配345:就像子字符串一样,但仍然允许23 VAL: like ones, use。
\b(?=[0-9. ]*[A-Z])[A-Z0-9][A-Z0-9. ]*:
See this regex demo. Here, \b(?=[0-9. ]*[A-Z])
matches a word boundary first, and then the positive lookahead (?=[0-9. ]*[A-Z])
makes sure there is an uppercase letter after 0+ digits, spaces or dots.
看到这个正则表达式演示。在这里,\ b(? =[0 - 9。[a - z])首先匹配一个词的边界,然后是正的前视(?=[0-9]。[A-Z])确保在0+数字、空格或点之后有一个大写字母。
If you do not expect numbers at the start of the sequence, i.e. out of I have a 22 C. GRAY CAT2:
, you need to extract C. GRAY CAT2
, use Sebastian's suggestion (demo).
如果你不希望数字出现在序列的开始,例如我有一个22 C. GRAY CAT2:,你需要提取C. GRAY CAT2,使用Sebastian的建议(demo)。
#2
0
Have revised the selected answer to my own question to cover the German special characters.
将选定的答案修改为我自己的问题,以覆盖德国的特殊字符。
[A-Z][A-Z0-9.ÜÄÖß ]*:
This appears to work, however the Germans have recently added a capital ß to their alphabet, which is surely not on most keyboards yet, and therefore will not be a problem for the RegEx for a while.
这似乎工作,然而德国最近增加了一个资本ß字母,这肯定不是大多数键盘,因此将不会是一个问题的正则表达式。