简体   繁体   English

使用正则表达式将单词分隔为带有分隔符的文本

[英]isolating words using regex expression to text with separators specific

I want to isolate ex. 我想隔离前。 3 word into long text 3个字变成长文字

//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 在这一点上,我想用正则表达式将时间隔离到括号中,但我要以这种格式返回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: 我尝试用3个字符串'\\).*' replace with ' ' '.*\\(' replace with ' ''---' replace with ', '问题是,文本中可能还有其他括号,那么它将无法像下面这样工作:

(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. 我仅限于替换操作,因为yahoo管道不是一种编程语言。

You can only extract the strings that look like numbernumbercolumnnumbernumbernumber : 您只能提取类似于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? 只有3次吗? Or possibly 2 or 4? 还是2或4?

This version assumes there are always 3 times in the parentheses you wanna keep, everything else in front of it gets captured. 此版本假定您想保留的括号中总是有3次,而前面的其他所有内容都将被捕获。

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

正则表达式可视化

Debuggex Demo Debuggex演示

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" 因此,这正则表达式说“行了比赛开始时选择了一切的时代,那么一切,是不是一个(或一(后面没有3时间符号,只有空白( \\s )和/或-它们之间”

After that you can do: 之后,您可以执行以下操作:

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

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

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