简体   繁体   English

在 Python pandas 中使用正则表达式查找组合数字和字母的特定字符序列

[英]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.

相关问题 正则表达式使用Python正则表达式查找特定数字 - Regex to find specific number using Python regex 使用正则表达式(Python)在特定字符序列后拆分字符串 - Splitting string after specific sequence of characters using regex (Python) 使用Python正则表达式查找以特定字母开头和结尾的单词 - Using Python regex find words starting and ending with specific letters 正则表达式 Python,除了字母序列 - Regex Python except a sequence of letters 使用第二列中的值从 Pandas 列中删除特定数量的字母,Python - delete specific number of letters from pandas column using values from second column, Python 正则表达式匹配字母,数字和一些特定字符? - Regex to match letters, numbers and some specific characters? 想要使用python在正则表达式中包含特定字符 - want to include specific characters in regex using python 如何检索仅遵循特定数字序列的行? Python Pandas - how to retrieve rows that only follow a specific number sequence ? Python Pandas Python 正则表达式查找 3 个数字的序列,后跟特定字符串 - Python regex find sequence of 3 numbers followed by specific string 正则表达式:获取以特定字母开头的所有数字和特殊字符,当数字后出现空格时停止 - Regex: get all numeric and special characters starting with specific letters, stop when space occurs after number
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM