正则表达式根据/进行拆分

时间:2021-12-06 21:40:26

I have a string like

我有一根像绳子的

abc123/xyz456/abc/123/aaa/zzz 

I would like to split it as

我想把它分成两份

abc123,  xyz456 and abc/123/aaa/zzz

Basically, I want to split and get the first 2 items as separate items and the rest of the items can be combined.

基本上,我想将前两项分割为两个单独的项,其余的项可以合并。

I tried the regex ^(.*)\/(.*)\/(.*)$ but it gives me the other way around like zzz, aaa and abc123/xyz456/abc/123.

我试着regex ^(. *)\ / \ /(. *),(. *)美元但是我反过来喜欢打鼾声,aaa和abc123 / xyz456 / abc / 123。

Is there a way to get this parsed from left to right instead of right to left? or any suggestions would be helpful

有没有一种方法可以从左到右而不是从右到左进行解析?或者任何建议都是有益的。

2 个解决方案

#1


2  

The easiest fix will be to modify the first two .* to .*? to make the first two .* lazy (that is, match up to the first occurrence of the subsequent subpattern):

最简单的解决方法是修改前两个。*?懒(即匹配后续子模式的第一次出现):

^(.*?)\/(.*?)\/(.*)$
 ^^^^^^^^^^^

See the regex demo

看到regex演示

Else, use negated character class [^\/] that matches any char but /:

别的,使用否定字符类[/ ^ \]匹配任何字符但/:

^([^\/]*)\/([^\/]*)\/(.*)$

See another demo

看到另一个演示

Note that * quantifier matches zero or more occurrences of the quantified subpattern, you need to replace it with + (one or more occurrences) if you need to match at least one.

注意,* quantifier匹配零个或多个量化子模式,如果需要匹配至少一个,则需要用+(一个或多个)替换它。

Note that the last .* can remain as is, a greedy pattern, as it is at the pattern end and will match as many chars (other than line break chars) as possible, up to the string end. Turning it to .*? (i.e. .*?$) makes no sense as it will slow down matching.

注意,最后的。*可以保持为贪婪模式,因为它在模式结束处,并将匹配尽可能多的字符(除换行字符),直到字符串结束。把它。* ?(例如,*?$)毫无意义,因为它会减慢匹配速度。

#2


0  

Try the following:

试试以下:

^([^\/]+)\/([^\/]+)\/(.*)$

#1


2  

The easiest fix will be to modify the first two .* to .*? to make the first two .* lazy (that is, match up to the first occurrence of the subsequent subpattern):

最简单的解决方法是修改前两个。*?懒(即匹配后续子模式的第一次出现):

^(.*?)\/(.*?)\/(.*)$
 ^^^^^^^^^^^

See the regex demo

看到regex演示

Else, use negated character class [^\/] that matches any char but /:

别的,使用否定字符类[/ ^ \]匹配任何字符但/:

^([^\/]*)\/([^\/]*)\/(.*)$

See another demo

看到另一个演示

Note that * quantifier matches zero or more occurrences of the quantified subpattern, you need to replace it with + (one or more occurrences) if you need to match at least one.

注意,* quantifier匹配零个或多个量化子模式,如果需要匹配至少一个,则需要用+(一个或多个)替换它。

Note that the last .* can remain as is, a greedy pattern, as it is at the pattern end and will match as many chars (other than line break chars) as possible, up to the string end. Turning it to .*? (i.e. .*?$) makes no sense as it will slow down matching.

注意,最后的。*可以保持为贪婪模式,因为它在模式结束处,并将匹配尽可能多的字符(除换行字符),直到字符串结束。把它。* ?(例如,*?$)毫无意义,因为它会减慢匹配速度。

#2


0  

Try the following:

试试以下:

^([^\/]+)\/([^\/]+)\/(.*)$