[英]Find specific sequence of characters combining number and letters using regex in Python pandas
I am trying to find all rows in a pandas DataFrame for which the column col
takes values of the format 1234-XX-YYY
, where XX
is a placeholder for any two capital letters (AZ) and YYY
is a placeholder for any three numbers [0-9].我正在尝试在 Pandas DataFrame 中查找列col
采用1234-XX-YYY
格式值的所有行,其中XX
是任意两个大写字母 (AZ) 的占位符, YYY
是任意三个数字的占位符 [ 0-9]。
Here is my code so far到目前为止,这是我的代码
How can I achieve the desired result?我怎样才能达到预期的结果?
df[df['col'].str.contains('^1234-\[A-Z]{2}\[d]{3}', na=False)]
When you escape an open [
you tell the regex engine to match it as a literal character.当您转义 open [
您会告诉正则表达式引擎将其作为文字字符进行匹配。 If you expect a -
to appear at some place in the string, you need to add it to the pattern.如果您希望-
出现在字符串中的某个位置,则需要将其添加到模式中。 Also, if you expect uppercase letters to appear, you need AZ
, not az
.此外,如果您希望出现大写字母,则需要AZ
,而不是az
。
Use用
^1234-[A-Z]{2}-[0-9]{3}$
Details细节
^
- start of string ^
- 字符串的开始1234-
- a literal string 1234-
- 文字字符串[AZ]{2}
- two uppercase letters [AZ]{2}
- 两个大写字母-
- a hyphen -
- 一个连字符[0-9]{3}
- three digits [0-9]{3}
- 三位数$
- end of string. $
- 字符串的结尾。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.