Solve your biggest tech problems alongside global tech experts with 1:1 help.
(?<=--- ty, ).*?(?=,)(?=.*(?:\*\*.*?){0})
(?<=--- ty, ).*?(?=,)(?=.*(?:\*\*.*?){1})
(?<=--- ty, ).*?(?=,)(?=.*(?:\*\*.*?){2})
(?<=--- ty, ).*?(?=,)(?=.*(?:\*\*.*?){3})
---
This will exclude the last n dv's from the results.(?s)^.*?Job dv:532.*?--- ty, (\d+).*$
The (?s) activates single line mode.$1
$devicenum = "532";
$my_ty_number = preg_replace("#^.*?Job dv:{$devicenum}.*?--- ty, (\d+).*$#s", "$1", $data);
(activated single line mode with the pattern modifier after the pattern, rather than within the pattern)(?<=(\*\*[^*]*?){2})(?<=--- ty, ).*?(?=,)
(?<=(\*\*[^*]*?){3})(?<=--- ty, ).*?(?=,)
(?<=(\*\*[^*]*?){4})(?<=--- ty, ).*?(?=,)
(?<=(\*\*[^*]*?){3})(?<=--- ty, ).*?(?=,) will prevent the first 2 dvs from matching.<?
$data = 'JE: ** Job dv:9987 . a1:311 a2:565 a3:1204 --- ty, 311762, --- a5, 31178747, --- b3, 31178384, ---a7, 31178381, --- a1, 15808387, --- ty, 3184, --- a5, 12045-05, ** Job dv:532 a1:31321 a2:5654 a3:1204 --- ty, 311762, --- a2, 3117678747, ---a5, 3113378384, --- a1, 3117yy658381, --- ty, 158533308387, --- ty, 334184, --- a5, 120456-05, **Job dv:456 . a1:31231 a2:565 a3:12054 --- ty, 3141762, --- a5, 4311748747, --- b3, 311784384, ---a7, 311478381, --- a1, 158048387, --- ty, 344184, --- a5, 1442045-05, ** Job dv:45654 a1:3441321 a2:56544 a3:124404 --- ty, 31174642, --- a2, 311767874447, ---a5, 311434378384, --- a1, 3117344658381, --- ty, 15855533308387, --- ty, 33434184, --- a5, 120456-05, ';
$dv_to_get = '532';
$dv = preg_replace("#^.*(Job dv:{$dv_to_get}(?:(?!Job dv).)*).*$#s", '$1', $data);
print "\$dv: $dv\n";
preg_match_all('#--- ty, (\d+)#', $dv, $matches);
print "ty's:";
print_r($matches[1]);
Result:$dv: Job dv:532 a1:31321 a2:5654 a3:1204 --- ty, 311762, --- a2, 3117678747, ---a5, 3113378384,
--- a1, 3117yy658381, --- ty, 158533308387, --- ty, 334184, --- a5, 120456-05, **
ty's:Array
(
[0] => 311762
[1] => 158533308387
[2] => 334184
)
(?<=(\*\*[^*]*?){3})(?<=--- ty, ).*?(?=,)(?=.*(?:\*\*.*?){1}) will restrict the matching to the 3rd dv (456)
(?<=(\*\*[^*]*?){2})(?<=--- ty, ).*?(?=,)(?=.*(?:\*\*.*?){2}) will restrict the matching to the 2nd dv (532)
(?<=--- ty, )\d*(?!.*\*\*)
31174642
15855533308387
33434184
using this as test data:JE: ** Job dv:9987 . a1:311 a2:565 a3:1204 --- ty, 311762, --- a5, 31178747, --- b3, 31178384, ---a7, 31178381, --- a1, 15808387, --- ty, 3184, --- a5, 12045-05, ** Job dv:532 a1:31321 a2:5654 a3:1204 --- ty, 311762, --- a2, 3117678747, ---a5, 3113378384, --- a1, 3117yy658381, --- ty, 158533308387, --- ty, 334184, --- a5, 120456-05, **Job dv:456 . a1:31231 a2:565 a3:12054 --- ty, 3141762, --- a5, 4311748747, --- b3, 311784384, ---a7, 311478381, --- a1, 158048387, --- ty, 344184, --- a5, 1442045-05, ** Job dv:45654 a1:3441321 a2:56544 a3:124404 --- ty, 31174642, --- a2, 311767874447, ---a5, 311434378384, --- a1, 3117344658381, --- ty, 15855533308387, --- ty, 33434184, --- a5, 120456-05,
What are the 3 and 1 in the first line, and the 2 and 2 in the second line? How I can modify to always refers to the last item?See here: http://www.experts-exchange.com/Programming/Languages/Regular_Expressions/Q_28417719.html#a40016728
If you are experiencing a similar issue, please ask a related question
Join the community of 500,000 technology professionals and ask your questions.