Java: How to extract a substring between two characters from a string?

I am relatively new to Java, and I need help extracting multiple substrings from a string. An example line is shown below:

String = "How/WRB can/MD I/PRP find/VB a/DT list/NN of/IN celebrities/NNS '/POS real/JJ names/NNS ?/."

Desired Result: WRB MD PRP VB DT NN IN NNS POS JJ NNS

I have a text file with possible thousands of similar POS-labeled lines that I need to extract POS tags and do some calculations based on POS tags.

I tried to use a tokenizer, but did not get the desired result. I even tried to use split()and store in arrays because I need to store and use it later, and this still does not work.

Finally, I tried using a wildcard template, and I am having trouble with regex because it returns a word with a slash.

Regex: [\/](.*?)\s\b
Result: /WRB /MD ....

, , - , .

+5
4

:

String string = "How/WRB can/MD I/PRP find/VB a/DT list/NN of/IN celebrities/NNS '/POS real/JJ names/NNS ?/.";
System.out.println(string.replaceAll("[^/]+/([^ ]+ ?)", "$1"));

: WRB MD PRP VB DT NN IN NNS POS JJ NNS .

+8

, . , , .

:

(?<=/).+?(?= |$)

, ,

, Java:

import java.util.regex.Pattern;
import java.util.regex.Matcher;
import java.util.LinkedList;

public class SO {
    public static void main(String[] args) {
        String string = "How/WRB can/MD I/PRP find/VB a/DT list/NN of/IN celebrities/NNS '/POS real/JJ names/NNS ?/.";
        Pattern pattern = Pattern.compile("(?<=/).+?(?= |$)");
        Matcher matcher = pattern.matcher(string);

        LinkedList<String> list = new LinkedList<String>();

        // Loop through and find all matches and store them into the List
        while(matcher.find()) { 
            list.add(matcher.group()); 
        }

        // Print out the contents of this List
        for(String match : list) { 
            System.out.println(match); 
        }
    }
}
+6
String string = "How/WRB can/MD I/PRP find/VB a/DT list/NN of/IN celebrities/NNS '/POS real/JJ names/NNS ?/.";

string = string .replaceAll("\\S+/", "").replace(".", "");  

System.out.println(string );
+2

str = str.repalceAll("\\S+/", "")? , .

0

All Articles