Best efficient way to split this pattern

70 views Asked by At

I have a file which includes thousands of lines like this:

node: { title: "0" label: "sub_401000" color: 76 textcolor: 73 bordercolor: black }

What I need is to extract the title value and label value. For example in the line above. I need to extract 0 and sub_401000. I can split them but it takes a lot of time. I want to know what would be the most efficient way to do this process?

2

There are 2 answers

6
James Wierzba On BEST ANSWER

Something like this should do (Note I assumed there to be one space between title: and quotes.

public class Test {

    public static void main(String[] args) 
    {
        String str = "node: { title: \"0\" label: \"sub_401000\" color: 76 textcolor: 73 bordercolor: black }";
        //String regex = ".*title: \"(.*)\".*label: \"(.*)\""; better regex below suggested by pschemo
        String regex = "title: \"([^\"]*)\".*label: \"([^\"]*)\"";
        Pattern p = Pattern.compile(regex);
        Matcher m = p.matcher(str);
        if(m.find())
        {
            String title = m.group(1);
            String label = m.group(2);
            System.out.println(title);
            System.out.println(label);
        }
    }
}

Output:

0

sub_401000

0
umbreon222 On

You could try using this regex and compile it if possible since it will be used repetitively. (Note: it is meant for capturing matches using the parenthesis)

 (\w+): "*(\w*)

Regular expression visualization

Debuggex Demo