1

How I can get fr and en from ["fr", "en"] using regex? I have tried to use split("[\\W]+") but it gives me 3 items ("", "fr" and "en").

How can I do this without getting that first empty string, and just get fr and en?

2 Answers 2

1
(?<=")[^",]+(?=")

Simply do a match instead of split.See demo.

https://regex101.com/r/pT4tM5/8

Sign up to request clarification or add additional context in comments.

Comments

1

Instead of split you should use this regex to match the desired parts:

(?<=")\w+(?=")

RegEx Demo

In Java:

String str = "[\"fr\", \"en\"]";
Pattern p = Pattern.compile("(?<=\")\\w+(?=\")");
Matcher = p.matcher(str);
while (m.find())
    System.out.println(m.group());

Output:

fr
en

3 Comments

i updated answer with complete Java code for your help.
any specific reason why you deleted your comment, so you don't deserve it
Sorry I didn't get, which comment I deleted?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.