简体   繁体   中英

Regex to match multiple occurrences of pattern and disregard content in-between

I am trying to match multiple occurrences of a pattern in a string and disregard the in-between content.

For example, in the string below:

"35264208011:5:1044512:0.1078, 1044512:6:3526415811:0.0444, 699905011:6:3526420011:0.0544”

I want to get all the occurrences of 1044512 and their accompanied rate (ie value with the decimal)

My desired output would be: "1044512:0.1078, 1044512:0.0444"

I have tried '1044512:(.*?)0\\0000' but seems to be missing something significant. Any help is much appreciated.

try this regex :

(1044512)(?:.*?)(0\.\d*)

in your example this will give you this matches :

match 1 : 1044512:0.1078
     group 1 : 1044512
     group 2 : 0.1078
match 2 : 1044512:6:3526415811:0.0444
     group 1 : 1044512
     group 2 : 0.0444

then all what you need to do is to concatenate group 1 with group 2 like this :

$1:$2

this is a 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