Regex 当h和m是可选的时,正则表达式获取歌曲h:m:s
模式:Regex 当h和m是可选的时,正则表达式获取歌曲h:m:s,regex,Regex,模式: (.*?) (?:(\d+)\:)??(?:(\d+)\:)??(\d+) 输入 替换: the song "$1" is $3min and $4sec 我想要的结果 the song "song1.mp3" is 2min and 35sec the song "song2.mp3" is 2min and 45sec the song "song3.mp3" is min and 45sec 所发生的是分钟部分试图不发生,并且由于某种原因,在空格出现之前,最左边的非reedy
(.*?) (?:(\d+)\:)??(?:(\d+)\:)??(\d+)
输入
替换:
the song "$1" is $3min and $4sec
我想要的结果
the song "song1.mp3" is 2min and 35sec
the song "song2.mp3" is 2min and 45sec
the song "song3.mp3" is min and 45sec
所发生的是分钟部分试图不发生,并且由于某种原因,在空格出现之前,最左边的非reedy模式(*?
),而不是最右边的非reedy模式((?:(\d+):)??
)
这看起来很奇怪。我试过使用flash和JS
我可以在结尾使用$
修复它,但是如果我不想匹配结尾怎么办<代码>*$将其打断
我最关心的是.NET和JS解决方案。以下内容似乎适用于您的所有示例,替换匹配项:
(.*?) (\d+:)?(\d+):(\d+)
与:
尝试以下模式:
(?<song>[^ ]+) (?:(?:(?<h>\d+):)?(?:(?<m>\d+):))?(?<s>\d+)
(?[^]+)(?:(?:(?\d+))(?:(?\d+))(?:(?\d+))(?\d+)
(..\.\w+(\d+)?(\d+)(\d+)
在regexpal中工作。您使用的语言是什么?@Patashu您的评论看起来像是一个答案-为什么不用这个来回答?@Patashu:这不是做分钟选项的标题说h和m是可选的。。。需要另一个?
吗?标题是这样说的,但问题中提供的示例都提供了分钟。是的,但我仍然希望它是可选的。我编辑了这个例子。但是已经有一个公认的答案了!我需要做的就是添加更多的括号!?我从来没有想到过。现在我相信??又没用了;)
the song "$1" is $3min and $4sec
(?<song>[^ ]+) (?:(?:(?<h>\d+):)?(?:(?<m>\d+):))?(?<s>\d+)