正则表达式匹配不匹配的组

时间:2018-08-06 23:47:53

标签: java regex regex-group regex-greedy

我想在全名中匹配某些名称,即全名+某些信息,例如发行年份或作者。 我想在fullName中获取realName。

[author1] Shuu [Digital]
[author1] Paradise ~★  Special Edition
 (magazin) [kulmov_ (Kurowa)] Channel (Fate/Grand Order) [Chinese] [*'N]
(COMIC1☆7) [ComicCon] X-Men!! (Marvel)
(magazin) [Rave (Jacky)] SPLASH 11 (Microsoft)
[anotherauthor] name1 | name2 (Hatsujou Complex) [English]
[author7 (aurhot234)] Connect (Project) [Digital]
[author3] ~Hellverse~ (COMICcon 2017)
[author4] Escape [English] [Decensored]

我发现名称几乎总是在“]”或“)”之后。然后是一个空格,然后是名称。名称后面是一个空格,不包含任何空格或“(”或“ [”。 名称可以包含一些非ASCII吗?符号。全名可以包含日文符号。

我发现:

]\s+(.+)+\s*[\(|\[|\s]*

我不知道为什么,但是它确实与“]” util匹配,包括字符串的结尾。 但正如我所看到的,它只应匹配“]”之后的所有内容,获取realName,然后在找到“(”或“ [”之后停止。

我该如何解决?

2 个答案:

答案 0 :(得分:1)

  

我不知道为什么,但是它确实与“]” util匹配,包括字符串的结尾。但正如我所看到的,它只应匹配“]”之后的所有内容,获取realName,然后在找到“(”或“ [”之后停止。

如果您希望它在看到counts = Child.objects.filter(parent=OuterRef('pk')) counts = counts.annotate(num_grandchildren=Count('grandchild_set')) counts = counts.order_by('-num_grandchildren').values('num_grandchildren')[:1] qs = Parent.objects.annotate(min_num_grandchildren=Subquery(counts)) Workbooks.OpenText FileName:=fpathO, datatype:=xlDelimited, comma:=True, local:=True 时停止,但没有必须为一个,或者可能不止一个,那么您需要对匹配的内容更加挑剔。 (将匹配任何字符序列(并且[中的第二个.+是多余的),并且可以包含+(.+)+字符。这样的事情会更好:

(

捕获组中不能包含任何[]\s+([^[(]+) 字符,因此,如果有任何字符,则匹配必然会在之前停止。

答案 1 :(得分:1)

我认为您可能正在寻找此正则表达式:

]\s+([^([\s]+(?:\s+[^([\s]+)*)

有关演示,请参见regex101.com。结果是:

Shuu
Paradise ~★  Special Edition
Channel
X-Men!!
SPLASH 11
name1 | name2
Connect
~Hellverse~
Escape

说明

]\s+             Match lead-in, i.e. end-bracket and some spaces
(                Start capture group:
  [^([\s]+         Match anything except '(', '[', and spaces
  (?:              Start optional repeating group:
    \s+              Match one or more spaces
    [^([\s]+         Match anything except '(', '[', and spaces
  )*               End of repeating group
)                End of capture group
相关问题