简体   繁体   English

正则表达式以匹配特殊字符之间的第一和第三组数字

[英]Regex to match first and third groups of numbers between special characters

I am currently working on a large data processing script in Python that uses subprocesses to extract strings of text from files. 我目前正在使用Python处理大型数据处理脚本,该脚本使用子流程从文件中提取文本字符串。 The strings that I am receiving are in the format of: 我收到的字符串的格式为:

-R##/##/##/##

An example of this would be -R-120/-115/-30/-20 (the four numbers can be either positive or negative) 例如-R-120/-115/-30/-20 (四个数字可以是正数或负数)

I am trying to come up with a regex expression to match the first and third numbers, so for the example above I would need -120 and -30 . 我试图提出一个正则表达式来匹配第一个和第三个数字,因此对于上面的示例,我需要-120-30

Could anyone steer me in the right direction for a clean way to go about doing this? 有人可以引导我朝着正确的方向前进,以一种干净的方式进行此操作吗?

You can use this regex in python with 2 capturing groups to grab your numbers: 您可以在带有2个捕获组的python中使用此正则表达式来获取您的数字:

>>> s = '-R-120/-115/-30/-20'
>>> print re.findall(r'^\D*?([-+]?\d+)\D*?[-+]?\d+\D*?([-+]?\d+)', s)

[('-120', '-30')]

RegEx Demo 正则演示

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

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