-1

I have one input String like this:

"I am Duc/N Ta/N Van/N" 

String "/N" present it is the Name of one person.
The expected output is:

Name: Duc Ta Van

How can I do it by using regular expression?

8
  • Do you want Name: Duc Ta Van as result or /N: Duc Ta Van? Are the literals Duc, Ta and Van always the same? Commented May 3, 2018 at 9:04
  • 1
    Can you at least try a bit ? We are not here to provide you the solution but to help you find it... Without an effort, I don't want to help you. See How to Ask Commented May 3, 2018 at 9:04
  • @ zᴉɹɥƆ: yeap, I would like Name: Duc Ta Van as the result Commented May 3, 2018 at 9:07
  • u can call a replace the \n whit another charArray String.replace("\\n", " ") Commented May 3, 2018 at 9:08
  • 2
    Urgency ? Unless this is an for an health application linked to an important surgery equipment, there is no urgency... See the duplicate to see how to use a regex to "replace" values in a String. The regex is simple to write after that. Commented May 3, 2018 at 9:10

3 Answers 3

3

You can use Pattern and Matcher like this :

String input = "I am Duc/N Ta/N Van/N";
Pattern pattern = Pattern.compile("([^\\s]+)/N");
Matcher matcher = pattern.matcher(input);
String result = "";
while (matcher.find()) {
    result+= matcher.group(1) + " ";
}

System.out.println("Name: " + result.trim());

Output

Name: Duc Ta Van

Another Solution using Java 9+

From Java9+ you can use Matcher::results like this :

String input = "I am Duc/N Ta/N Van/N";
String regex = "([^\\s]+)/N";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(input);
String result = matcher.results().map(s -> s.group(1)).collect(Collectors.joining(" "));

System.out.println("Name: " + result); // Name: Duc Ta Van
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks a lot @YCF_L
That java 9 solution is excellent. I really need to update my systems!
This is correct @AxelH it is the time for us to move to Java 10 :)
3

Here is the regex to use to capture every "name" preceded by a /N

(\w+)\/N

Validate with Regex101

Now, you just need to loop on every match in that String and concatenate the to get the result :

String pattern = "(\\w+)\\/N";
String test = "I am Duc/N Ta/N Van/N";
Matcher m = Pattern.compile(pattern).matcher(test);

StringBuilder sbNames = new StringBuilder(); 
while(m.find()){
    sbNames.append(m.group(1)).append(" ");
}
System.out.println(sbNames.toString());

Duc Ta Van

It is giving you the hardest part. I let you adapt this to match your need.

Note :
In java, it is not required to escape a forward slash, but to use the same regex in the entire answer, I will keep "(\\w+)\\/N", but "(\\w+)/N" will work as well.

1 Comment

Yeah I notice that... strangely regex101 asked for "/" to be escaped ... Since it was working I kept it in the java to use the same regex and kept the link to show the dynamic match. But thanks @YCF_L EDIT: this is the delimiter for the flags... I am stupid ;) java doesn't support them.
0

I've used "[/N]+" as the regular expression.

Regex101

[] = Matches characters inside the set

\/ = Matches the character / literally (case sensitive)

+ = Matches between one and unlimited times, as many times as possible, giving back as needed (greedy)

6 Comments

P.S. This regex can be used to remove /N out of the string.
You will still find the rest of the String that is not followed by /N and that is not a name. n the example I am is not needed.
AxelH I haven't touched regex since 2nd year of college, my bad :)
That's not about the regex, but about the question itself. It is not only the remove /N but to extract the word before every /N.
Ahh okay. I re-read the question there! Cheers. Does (\w+) do the actual grouping/extraction?
|

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.