2017-08-24 102 views
2

我正在尝试开发一个正则表达式,它在连字符的第一个实例之前拉出前几个字符,然后在第一个连字符后保存第二组元素。排除第一个连字符后的连字符

这里的正则表达式:

^([^-]*)(?(?=-)(\S.*)|()) 

而且这里有几个测试案例:

SSB x Dj Chad - Crazy Beat - Tarraxo 
Dj [R]afaa [F]ox -Tarraxo Do Inicio Das Aulas (Nova Escola Producões) 
Dj Snakes Share - MaloncyBeatz - Perfecto 
Tarraxo Das Brasileiras [2014] [TxiGa Pro] 

IF语句处理最后一个条件很好,但我的问题是,在最初的几个项目,则返回第二组使用连字符而不是排除它。

换句话说: Dj Snakes Share - MaloncyBeatz - Perfecto应该返回:

  • 组1:Dj Snakes Share
  • 组2:MaloncyBeatz - Perfecto

相反,第2组为:- MaloncyBeatz - Perfecto

更新

https://regex101.com/r/2BQPNg/12

使用^([^-]*)[^-]\W*(.*),它的工作原理,但它提出的最后一种情况下(在没有连字符)的问题。它不包括]

+0

好像它是您的测试链接,现在的工作。 – tima

+0

@tima最后一种情况现在排除字符串 – Adib

回答

3

我的解决办法:

^([^-]+?)\s*(?:-\s*(.*))?$

^   // start of line 
([^-]+?) // 1+ not '-' chars, lazily matched (first captured group) 
\s*  // 0+ white-space chars 
(?:  // grouped, not captured 
-   // dash 
\s*(.*) // 0+ white-space chars then anything (second captured group) 
)?  // 0 or 1 time 
$   // end of line 

标志:全球多线

Demo

501步减少到164步:

^[^-]+$|^((?:\w[^-]*)?\w)\W+(\w.*)

^    # start of line 
[^-]+   # 1 or more not '-' 
$    # end of line 
|    # OR 
^    # start of line 
(    # start of group (captured) 
(?:    # start of group (not captured) 
\w[^-]*   # a word char then 0 or more not '-' 
)?    # 0 or 1 times 
\w)    # a word char, then end of group 
\W+    # 1 or more non-word chars 
(\w.*)   # a word char then 0 or more anything (captured) 

Demo

+0

这满足所有条件!谢谢! – Adib

+0

随着数据增长,这将非常缓慢。 [目前](https://regex101.com/r/GEEgEp/3)与[我的正则表达式](https://regex101.com/r/2BQPNg/15)相比,它需要595个步骤,需要76个步骤。 – anubhava

+0

@anubhava只是[删除懒惰匹配](https://regex101.com/r/GEEgEp/4),这个答案是在你的前面16步! –

1

您正在使用此正则表达式:

^([^-]*)[^-]\W*(.*) 

在这里,你在你的正则表达式,是造成第一组匹配一个字符不到比赛的额外[^-]

你可以使用这个表达式:

^([^-]*)(?:\s+-\s*(.*))?$ 

RegEx Demo

+0

末尾的']',我基本上不用[^ - ],最好是简单地使用\ W *来捕获所有非字母字符。到目前为止,我没有任何问题,但你的正则表达式如何处理这种情况:'Dj蛇分享 - [MaloncyBeatz] - Perfecto' – Adib

+1

好吧,现在检查我的更新答案。 – anubhava

相关问题