简体   繁体   English

正则表达式捕获字符串中的确切数字

[英]Regex to capture exact numbers in string

I have a column that looks like this:我有一个看起来像这样的列:

11/33/4500030050
4100000300/4503134501
4100030300+4503114501
11

The regular expression should capture the following:正则表达式应捕获以下内容:

4500030050
4100000300/4503134501
4100030300+4503114501
''

Here's my current regular expression:这是我当前的正则表达式:

 col.str.findall(r'[/+ #_;.-]?(?<![0-9])[0-9]{10}(?![0-9])').str.join('').str.lstrip('/+ #_;.-')

This however captures all numbers that have 10 digits.但是,这会捕获所有具有 10 位数字的数字。 How can I modify so that it can only capture numbers that start with 41 and 45?如何修改,使其只能捕获以 41 和 45 开头的数字?

You can use您可以使用

[+ #_;.-]?(?<![0-9])4[15][0-9]{8}(?![0-9])

See the regex demo .请参阅正则表达式演示

Details :详情

  • [+ #_;.-]? - an optional + , space, # , _ , ; - 可选的+ 、空格、 #_; , . , . or --
  • (?<![0-9]) - left digit boundary (?<![0-9]) - 左数字边界
  • 4 - a 4 digit char 4 - 一个4位字符
  • [15] - 1 or 5 [15] - 15
  • [0-9]{8} - eight digits [0-9]{8} - 八位数字
  • (?![0-9]) - right digit boundary. (?![0-9]) - 右数字边界。

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

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