-1

I have successfully written a regex pattern to scrape some value and it works really awesome but i am not getting how to name the capture group.

This is my string:

<div class="pipe-mailbody pipe-mailbody-2cccfb01-75f1-4fc0-9d5f-5f5ed8729d1b">Deal ID:<br>256<br><br>Deal pipeline ID:<br>3<br><br>Deal stage ID:<br>16<br><br>Deal contact person ID:<br>740<br><br>End:</div>

and this is my pattern:

Deal\sID\:\<br\>([\d]+)\<br\>[\<\>\d\w\s]+\:\<br\>([\d]+)<br><br>Deal\sstage\sID:<br\>([\d]+)<br\>[\d\s\>\<\S]+person\sID\:\<br>([\d]+)\<br\>

Can anyone help me how to name the capture group? like ([\d]+) this is the one of capture group, i want to name it person

How can i achieve it?

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563

1 Answers1

-1

All right, well I'm not sure if this is what you are looking for, but hopefully this helps.

You can use something like a Pattern Matcher in Java to get a value. A Pattern object in Java allows you to compile a regex pattern. The Matcher object then gets all matches of the pattern from a certain string. You can get a capture group using the group method of the Matcher object.

If this is your string:

<div class="pipe-mailbody pipe-mailbody-2cccfb01-75f1-4fc0-9d5f-5f5ed8729d1b">Deal ID:<br>256<br><br>Deal pipeline ID:<br>3<br><br>Deal stage ID:<br>16<br><br>Deal contact person ID:<br>740<br><br>End:</div>

With Java, you can do this (as a note, I also simplified your pattern, as there were a number of unnecessary escape characters):

String input = "<div class="pipe-mailbody pipe-mailbody-2cccfb01-75f1-4fc0-9d5f-5f5ed8729d1b">Deal ID:<br>256<br><br>Deal pipeline ID:<br>3<br><br>Deal stage ID:<br>16<br><br>Deal contact person ID:<br>740<br><br>End:</div>";

Pattern p = Pattern.compile("Deal\sID:<br>(\d+)<br>.+:<br>(\d+)<br><br>Deal\sstage\sID:<br>(\d+)<br>.+person\sID:<br>(\d+)<br>";

Matcher m = p.match(input);

String person = m.group(4);

This saves the person ID in a String variable.

user11809641
  • 815
  • 1
  • 11
  • 22