简体   繁体   中英

isolating words using regex expression to text with separators specific

I want to isolate ex. 3 word into long text

//more text
word word word (15:00 - - - 16:00 - - - 21:00)
//text ...

At this point i want to isolate times into parenthesis with a regex expression but i want to return in this format 15:00,16:00,21:00

i have tried with 3 strings '\\).*' replace with ' ' '.*\\(' replace with ' ' and '---' replace with ', ' the problem is, that there may be other brackets in the text, and then it won't work like here:

(word) word word (19:00 - - - 20:00 - - - 21:00) asd asd asd (text text)

I'm limited to replace-operations because yahoo pipes is not a programming language.

You can only extract the strings that look like numbernumbercolumnnumbernumbernumber :

var result = str.match(/\b\d\d:\d\d\b/g);

result will be an array with all the matchs

demo

So you are limited to doing replace-operations? Because extraction would make more sense here.

The requirements and variables aren't very clear. Apparently (from the comments) there could be parentheses in front of and behind the one you wanna keep. Are there only ever 3 times? Or possibly 2 or 4?

This version assumes there are always 3 times in the parentheses you wanna keep, everything else in front of it gets captured.

^(?:[^(]|\((?!(?:\d{1,2}:\d{2}[\s-]*){3}))*\(

正则表达式可视化

Debuggex Demo

So this regex select everything up to the times by saying "match start of the line, then everything that isn't a ( or a ( that isn't followed by 3 time-notations with only whitespace ( \\s ) and/or - between them"

After that you can do:

'\).*' replace with ''
'[\s-]+' replace with ', '

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