Save parts of a String in different variables using regular expressions and java split function

2

I have a problem with these regular expressions. Based on the following code and knowing that the content of wemcamDetails is:

USB\VID_04F2&PID_B2E1&MI_00&9F9657C&0&1200                : Lenovo EasyCamera

Code:

String line = "";
if ((line = reader.readLine())!= null) {
    if (line.startsWith("USB")){
        webcamDetails = line;
    }
}

String regEx1 = "^\S*";
String regEx2 = "\:(.*)";

String[] webcamID = webcamDetails.split(regEx1);
System.out.println("Device ID: "+webcamID[0]);

String[] webcamDeviceName = webcamDetails.split(regEx2);

System.out.println("Device Name: "+webcamDeviceName[0]);

I want to save USB\VID_04F2&PID_B2E1&MI_00&9F9657C&0&1200 in the variable webcamID, and Lenovo EasyCamera in webcamDeviceName . I have tested the regular expressions in several regex testers and they work, but for some reason that I do not know is not keeping me well in the variables, it always saves the first part of the String, but never saves the name of the camera. Can someone who controls me tell me what I'm doing wrong?

    
asked by Tofetopo 08.02.2017 в 19:48
source

1 answer

2

The problem is that you are using split() , so the text returned in webcamID[0] and webcamDeviceName[0] is what is found before the first match .

Instead, instead of using split (), we can use find () , with a single regular expression that:

  • Verify that it starts with USB .
  • Use groups (parentheses ) to capture the 2 texts independently.
  • Regular expression:

    ^(USB\S*)\s*:\s*(.*)
    

    Code:

    import java.util.regex.Matcher;
    import java.util.regex.Pattern;
    
    final String regex = "^(USB\S*)\s*:\s*(.*)";
    final String string = "USB\VID_04F2&PID_B2E1&MI_00\6&9F9657C&0&1200                : Lenovo EasyCamera";
    
    final Pattern pattern = Pattern.compile(regex);
    final Matcher matcher = pattern.matcher(string);
    
    if (matcher.find()) {
        String webcamID = matcher.group(1);
        String webcamDeviceName = matcher.group(2);
    
        System.out.println("Device ID: "+webcamID);
        System.out.println("Device Name: "+webcamDeviceName);
    }
    

    Result:

    Device ID: USB\VID_04F2&PID_B2E1&MI_00&9F9657C&0&1200
    Device Name: Lenovo EasyCamera
    

    Demo:

    link


    Alternative with split

    Another option that I would use split (), would be starting the text in:

    \s*:\s*
    

    That is, any number of spaces, followed by : and more optional spaces.

    final String regex = "\s*:\s*";
    final String string = "USB\VID_04F2&PID_B2E1&MI_00\6&9F9657C&0&1200                : Lenovo EasyCamera";
    
    String[] webcam = string.split(regex);
    
    System.out.println("Device ID: "+webcam[0]);
    System.out.println("Device Name: "+webcam[1]);
    

    Demo: link

    However, this would also split the string if it had a : symbol inside the ID or the Name . Also, it does not convince me: I recommend the first option.

        
    answered by 08.02.2017 / 20:26
    source