简体   繁体   中英

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. 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)

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 .

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:

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

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

RegEx Demo

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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