简体   繁体   English

正则表达式模式匹配字符串,如果仅包含某些允许的字符

[英]Regex pattern matching string iff it only contain certain allowed characters

I am new to regrex and trying to get a grasp of it. 我是新来的regrex并试图掌握它。

How could I write a regrex string that matches a string if and only if the string contains only certain allowed characters, let's say capital 'A', 'B', 'C', 'D' and 'E', ei it should match 'A', 'ABC', 'CEA', 'ABCEACBBCAED?, but not 'AGV' AcD', 'a', etc. (?) 当且仅当该字符串仅包含某些允许的字符时,我才能写一个与该字符串匹配的regrex字符串,比如大写字母'A','B','C','D'和'E',即它应该匹配'A','ABC','CEA','ABCEACBBCAED ?,但不是'AGV'AcD','a'等(?)

re.search(some_regex_string, some_string)

You need to use this regex for matching the kind of sample data in your post. 您需要使用此正则表达式来匹配帖子中的示例数据类型。

^[A-E]+$

Regex Demo 正则表达式演示

In case you have a larger string where you want to only match tokens which has only A to Z characters, then in that case you can use word boundaries around the regex as this, 如果您有一个较大的字符串,只想匹配只有AZ字符的令牌,那么在这种情况下,您可以在正则表达式周围使用单词边界,

\b[A-E]+\b

Which will match only strings that contain A to E characters into the string. 仅匹配包含AE字符的字符串。

Demo for matching intended strings in a larger string 演示如何匹配较大字符串中的预期字符串

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM