在每个路径上分割正则表达式

时间:2022-05-31 21:39:29

If I have this:

如果我有这个:

thisisgibberish  1234 /hello/world/
more gibberish 43/7 /good/timing/
just onemore    8888  /thanks/mate

what would the regular expression inside the Java String.split() method be to obtain the paths per line?

在Java String.split()方法中,获取每行路径的正则表达式是什么?

ie.

ie。

[0]: /hello/world/
[1]: /good/timing/
[2]: /thanks/mate

Doing

myString.split("\/[a-zA-Z]") 

causes the splits to occur to every /h, /w, /g, /t, and /m.

导致每个/h、/w、/g、/t和/m发生劈裂。

How would I go about writing a regular expression to split it only once per line while only capturing the paths?

如何编写一个正则表达式,在只捕获路径的情况下,每一行只分割一次?

Thanks in advance.

提前谢谢。

3 个解决方案

#1


3  

Why split ? I think running a match here is better, try the following expression:

为什么分手呢?我认为在这里运行一个匹配更好,试试下面的表达式:

(?<=\s)(/[a-zA-Z/])+

Regex101 Demo

Regex101演示

#2


0  

This uses split() :

它使用分裂():

String[] split = myString.split(myString.substring(0, myString.lastIndexOf(" ")));        

OR

myString.split(myString.substring(0, myString.lastIndexOf(" ")))[1]; //works for current inputs

#3


0  

You must first remove the leading junk, then split on the intervening junk:

你必须先除去主要的垃圾,然后在中间的垃圾上分开:

String[] paths = str.replaceAll("^.*? (?=/[a-zA-Z])", "")
    .split("(?m)((?<=[a-zA-Z]/|[a-zA-Z])\\s|^).*? (?=/[a-zA-Z])");

One important point here is the use of (?m), which is a switch that turns on "dot matches newline", which is required to split across the newlines.

这里的一个要点是(?m)的使用,它是一个打开“点匹配新行”的开关,这是跨换行所必需的。

Here's some test code:

这里有一些测试代码:

String str = "thisisgibberish  1234 /hello/world/\nmore gibberish 43/7 /good/timing/\njust onemore    8888  /thanks/mate";
String[] paths = str.replaceAll("^.*? (?=/[a-zA-Z])", "")
    .split("(?m)((?<=[a-zA-Z]/|[a-zA-Z])\\s|^).*? (?=/[a-zA-Z])");
System.out.println( Arrays.toString( paths));

Output (achieving requirements):

输出(实现需求):

[/hello/world/, /good/timing/, /thanks/mate]

#1


3  

Why split ? I think running a match here is better, try the following expression:

为什么分手呢?我认为在这里运行一个匹配更好,试试下面的表达式:

(?<=\s)(/[a-zA-Z/])+

Regex101 Demo

Regex101演示

#2


0  

This uses split() :

它使用分裂():

String[] split = myString.split(myString.substring(0, myString.lastIndexOf(" ")));        

OR

myString.split(myString.substring(0, myString.lastIndexOf(" ")))[1]; //works for current inputs

#3


0  

You must first remove the leading junk, then split on the intervening junk:

你必须先除去主要的垃圾,然后在中间的垃圾上分开:

String[] paths = str.replaceAll("^.*? (?=/[a-zA-Z])", "")
    .split("(?m)((?<=[a-zA-Z]/|[a-zA-Z])\\s|^).*? (?=/[a-zA-Z])");

One important point here is the use of (?m), which is a switch that turns on "dot matches newline", which is required to split across the newlines.

这里的一个要点是(?m)的使用,它是一个打开“点匹配新行”的开关,这是跨换行所必需的。

Here's some test code:

这里有一些测试代码:

String str = "thisisgibberish  1234 /hello/world/\nmore gibberish 43/7 /good/timing/\njust onemore    8888  /thanks/mate";
String[] paths = str.replaceAll("^.*? (?=/[a-zA-Z])", "")
    .split("(?m)((?<=[a-zA-Z]/|[a-zA-Z])\\s|^).*? (?=/[a-zA-Z])");
System.out.println( Arrays.toString( paths));

Output (achieving requirements):

输出(实现需求):

[/hello/world/, /good/timing/, /thanks/mate]