-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?

Youcef LAIDANI
  • 55,661
  • 15
  • 90
  • 140
AndroidTa
  • 79
  • 6
  • Do you want `Name: Duc Ta Van` as result or `/N: Duc Ta Van`? Are the literals Duc, Ta and Van always the same? – Chrᴉz remembers Monica May 03 '18 at 09: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 [ask] – AxelH May 03 '18 at 09:04
  • @ zᴉɹɥƆ: yeap, I would like Name: Duc Ta Van as the result – AndroidTa May 03 '18 at 09:07
  • u can call a replace the \n whit another charArray String.replace("\\n", " ") – Fabrizio Scarponi May 03 '18 at 09:08
  • Possible duplicate [Java; String replace (using regular expressions)?](https://stackoverflow.com/questions/632204/java-string-replace-using-regular-expressions) – AxelH May 03 '18 at 09:08
  • @AxelH: Can you privide me one recommend to solve it. Because of urgency – AndroidTa May 03 '18 at 09:09
  • 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. – AxelH May 03 '18 at 09:10
  • Please see: [Under what circumstances may I add “urgent” or other similar phrases to my question, in order to obtain faster answers?](https://meta.stackoverflow.com/q/326569) – Pshemo May 03 '18 at 09:15

3 Answers3

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.

AxelH
  • 14,325
  • 2
  • 25
  • 55
  • 1
    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. – AxelH May 03 '18 at 09:25
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
Community
  • 1
  • 1
Youcef LAIDANI
  • 55,661
  • 15
  • 90
  • 140
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)

Alex
  • 2,164
  • 1
  • 9
  • 27
  • P.S. This regex can be used to remove `/N` out of the string. – Alex May 03 '18 at 09:26
  • 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 May 03 '18 at 09:28
  • AxelH I haven't touched regex since 2nd year of college, my bad :) – Alex May 03 '18 at 09:30
  • 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`. – AxelH May 03 '18 at 09:32
  • 1
    Ahh okay. I re-read the question there! Cheers. Does `(\w+)` do the actual grouping/extraction? – Alex May 03 '18 at 09:33
  • Yep, that's one solution. (That's the one I used) – AxelH May 03 '18 at 09:34