Regular Expression Help [Java]

So i'm trying to write a regular expression to help me pull id numbers from a string, the input would look something like:

"Product Name" stock=6 sid=0x000000000034c000 pid=0xd70

What I'd like is a regular expression that I could enter that would return just the pid value (and one for the sid value too). The problem is I can't be sure the pid or sid will always be the same length, so rather than the input above I might get an entry that says:

"Different Product Name" stock=45 sid=0x000000000034c000 pid=0xa11713

So in the first one I'd like the expression to cut out 0xd70 and in the second one I'd like it to cut out 0xa11713. If it is any help I know that the pid is never more than 8 characters long after the 0x, and that the sid is never more than 16 (I'm not sure if shipping id can be shorter than 16 or not).

I'm new to using regular expressions, and I've figured out hot to match then number entry when I know the length, but I can't figure it out for a variable length.

input1: "Product Name" stock=6 sid=0x000000000034c000 pid=0xd70
input2: "Product Name" stock=6 sid=0x000000000034c000 pid=0xa11713
regex: pid=.....
output1: pid=0xd70
output2: pid=0xa11 <-- missing 713

Thanks for the help.

Create a pattern using

Pattern p = Pattern.compile( "sid=(0x[a-z0-9]+)\s+pid=(0x[a-z0-9]+)" );

and then match it against your input using

Matcher m = p.matcher( input );

m.group( 1 ) will then be the SID, m.group( 2 ) will be the PID.

1 Like

Oh, ok. So its the + sign that allows the expression to continue looking. Thank you.

Next question then would be, if I wanted to extract the product id directly with out the pid= infront of it, how would I do that? So for example:

input: "Product Name" stock=6 sid=0x000000000034c000 pid=0xd70
output: 0xd70

$
$
$ # Display the content of the Java program
$
$ cat -n ExtractSidPid.java
     1  import java.util.regex.Matcher;
     2  import java.util.regex.Pattern;
     3  public class ExtractSidPid {
     4    public static void main(String args[]) {
     5      String str = args[0];
     6      Pattern p = Pattern.compile("sid=([^ ]+) .*pid=([^ ]+)");
     7      // Try to match the string
     8      Matcher matcher = p.matcher(str);
     9      // Display both match groups
    10      while (matcher.find()) {
    11        System.out.println("SID = " + matcher.group(1));
    12        System.out.println("PID = " + matcher.group(2));
    13      }
    14    }
    15  }
$
$
$ # Compile
$
$ javac ExtractSidPid.java
$
$ # Run the program passing a test string as argument
$
$ java ExtractSidPid "Product Name stock=6 sid=0x000000000034c000 pid=0xd70"
SID = 0x000000000034c000
PID = 0xd70
$
$ # Once more
$
$ java ExtractSidPid "Different Product Name stock=45 sid=0x000000000034c000 pid=0xa11713"
SID = 0x000000000034c000
PID = 0xa11713
$
$
$

tyler_durden

1 Like

Thank you, I'm starting to get the hang of these expressions I think.

Oh I see now, the groups that pludi mentioned are the numbers. Ok, I didn't understand that. Thank you both very much.