1

I have an input and need to split and extract all Strings between /, using regexp.

I tried using: \/\s*(\w+)

This produces: /pathvalue1

Sample input:

/pathvalue1/path-value-one /pathvalue2/path-value /pathvalue3/pathvaluethree

Output that I need:

pathvalue1 path-value-one pathvalue2 path-value pathvalue3 pathvaluethree

Any hints? Much appreciated!

nightfixed
  • 871
  • 1
  • 12
  • 24
  • 3
    Why not simply split the string at the `/` using [`split()`](https://docs.oracle.com/javase/8/docs/api/java/lang/String.html#split-java.lang.String-)? – Ted Hopp Sep 01 '17 at 15:00
  • I seems more "professional" work done with `regexp` and I want to impress. Yeah, I know, is not the best argument. I am trying to learn `regexp` matching. – nightfixed Sep 01 '17 at 15:02
  • 1
    As the others said but for the sake of answering your question: `\/\s*([^/]+)` ;-) – Lothar Sep 01 '17 at 15:02
  • 2
    The "more professional" approach is to use the best tool for the job at hand. :) – Ted Hopp Sep 01 '17 at 15:02
  • `"/string1/string2".split("/");` ==> `["","string1","string2"]`, is that not enough? – Austin_Anderson Sep 01 '17 at 15:03
  • `\w+` doesn't contain the dash char - so why not simply adding it? Or just as @Lothar suggested, use everything except the slash `/` as character range – UninformedUser Sep 01 '17 at 15:04
  • As others have pointed out, this is not the best time to use regex. See: [When is it best to use regular expressions over basic string splitting/substring'ing](https://stackoverflow.com/questions/357814/when-is-it-best-to-use-regular-expressions-over-basic-string-spliting-substrin) or [When should I not use regular expressions](https://stackoverflow.com/questions/7553722/when-should-i-not-use-regular-expressions) – ctwheels Sep 01 '17 at 15:06

3 Answers3

1

I would use split approach as Kushan pointed in his answer. However if you still want to use a regex, then you can use a regex like this:

([\w-]+)  or
([a-z-]+) with insensitive flag

Working demo

Java code

String text = "/pathvalue1/path-value-one\n" +
              "/pathvalue2/path-value\n" +
              "/pathvalue3/pathvaluethree";

Matcher m = Pattern.compile("([\\w-]+)").matcher(text);

while (m.find()) {
    System.out.println(m.group(1));
}

IdeOne working demo

Output

pathvalue1
path-value-one
pathvalue2
path-value
pathvalue3
pathvaluethree
Federico Piazza
  • 30,085
  • 15
  • 87
  • 123
0

Use the String split function to split your string into an array

String[] yourarray = Yourstring.split("/");

This should split your path and give you your seperated values.

You will find your hyphen seperated value in the array as well.

Kushan
  • 5,855
  • 3
  • 31
  • 45
0

If you insist using regex than you can use this [^/\\s]+ with Pattern like this :

String[] strs = new String[]{"/pathvalue1/path-value-one",
    "/pathvalue2/path-value",
    "/pathvalue3/pathvaluethree"};
String regex = "[^/\\s]+";
for (String str : strs) {
    Pattern pattern = Pattern.compile(regex);
    Matcher matcher = pattern.matcher(str);

    while (matcher.find()) {
        System.out.println(matcher.group());
    }
}

Outputs

pathvalue1
path-value-one
pathvalue2
path-value
pathvalue3
pathvaluethree

regex demo


You can solve your problem also using split just one point if you use split str.split("/") directly than you can get this results :

[, pathvalue1, path-value-one]

To make sure that you get the correct result, then replace the no usefull / or spaces in the start of the url so you can use :

String[] strs = new String[]{"/pathvalue1/path-value-one",
    "/pathvalue2/path-value",
    "/pathvalue3/pathvaluethree"};
for (String str : strs) {
    System.out.println(Arrays.toString(str.replaceAll("^[/\\s]", "").split("/")));
}

Outputs

[pathvalue1, path-value-one]
[pathvalue2, path-value]
[pathvalue3, pathvaluethree]
Youcef LAIDANI
  • 55,661
  • 15
  • 90
  • 140