Link to home
Start Free TrialLog in
Avatar of joyacv2
joyacv2Flag for Puerto Rico

asked on

info between words

I have the following string

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,

I have a variation of a question ask before:

using this RegEx:

(?<=--- ty, ).*?(?=,)

How I can modify to obtain the numbers of the ty corresponding to a specific dv

Any idea?
Avatar of Dan Craciun
Dan Craciun
Flag of Romania image

What is a div?
Avatar of joyacv2

ASKER

dv is a name of a device
OK. I read div :)
Too late for me. Here's what I did so far:
(?<=--- ty, ).*?(?=,)(?=.*(?:\*\*.*?){0})
(?<=--- ty, ).*?(?=,)(?=.*(?:\*\*.*?){1})
(?<=--- ty, ).*?(?=,)(?=.*(?:\*\*.*?){2})
(?<=--- ty, ).*?(?=,)(?=.*(?:\*\*.*?){3})
---

Open in new window

This will exclude the last n dv's from the results.
So (?<=--- ty, ).*?(?=,)(?=.*(?:\*\*.*?){1}) will exclude the last dv, (?<=--- ty, ).*?(?=,)(?=.*(?:\*\*.*?){2}) will exclude the last 2 dvs and so on.

Will try again tomorrow, if no one manages to get a working regexp.

HTH,
Dan
Avatar of joyacv2

ASKER

Hi Dan,

I will wait for tomorrow to continue the problem, thanks!
I'd build the device number into the pattern like this, and do a replace:
(?s)^.*?Job dv:532.*?--- ty, (\d+).*$

Open in new window

The (?s) activates single line mode.

Replacing with:
$1

Open in new window


If you're using PHP this would be:
$devicenum = "532";
$my_ty_number = preg_replace("#^.*?Job dv:{$devicenum}.*?--- ty, (\d+).*$#s", "$1", $data);

Open in new window

(activated single line mode with the pattern modifier after the pattern, rather than within the pattern)

The result is 311762
@Terry: There can be multiple ty's in the same dv. For the 532 dv you chose, you have:
--- ty, 311762,
--- ty, 158533308387,
--- ty, 334184,
So, next step. This will remove the first n-1 dv's from matching:
(?<=(\*\*[^*]*?){2})(?<=--- ty, ).*?(?=,)
(?<=(\*\*[^*]*?){3})(?<=--- ty, ).*?(?=,)
(?<=(\*\*[^*]*?){4})(?<=--- ty, ).*?(?=,)

Open in new window

(?<=(\*\*[^*]*?){3})(?<=--- ty, ).*?(?=,) will prevent the first 2 dvs from matching.

Now let's see how we can combine those...
SOLUTION
Avatar of Dan Craciun
Dan Craciun
Flag of Romania image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Nice, Terry!

@joyacv2: So there you have it. 2 solutions, one that requires you to know the dv's position, the other that requires you to know the dv's name/id.
If you only want to parse the string, my solution is easier to parse in a loop, giving you each dv's ty's in an iteration.
If you're only interested in finding the ty's for a particular dv (for which you know the id), Terry's solution will give you that, without the need to know the position.
Thanks for your feedback Dan; you deserve a points split if my solution is accepted.
Avatar of joyacv2

ASKER

Hi Terry and Dan:

Hi Dan:

In this lines:
(?<=(\*\*[^*]*?){3})(?<=--- ty, ).*?(?=,)(?=.*(?:\*\*.*?){1}) will restrict the matching to the 3rd dv (456)

(?<=(\*\*[^*]*?){2})(?<=--- ty, ).*?(?=,)(?=.*(?:\*\*.*?){2}) will restrict the matching to the 2nd dv (532)

Open in new window


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?

Hi Terry:

Your code will always work with the last match?

To both:

I ask this because I have some cases that the Job dv: number is the same but the other numbers change. I always select the corresponding ty of the last Job dv


Thank You very much to both!
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of joyacv2

ASKER

Hi Dan,

This line of code not return nothing, can you check?
It's returning this in RegexBuddy, using PCRE or .NET engines:
31174642
15855533308387
33434184

Open in new window

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, 

Open in new window

Avatar of joyacv2

ASKER

sorry dan, let me check again, i make a mistake
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: https://www.experts-exchange.com/questions/28417719/info-between-words.html?anchorAnswerId=40016728#a40016728

You have 4 dv groups in your sample data, so n is 4.
Avatar of joyacv2

ASKER

Yesss, this return the last three corresponding to the last job dv,

I am using prey_match_all('/(?<=--- ty, )\d*(?!.*\*\*)/',$data,$res,PREG_PATTERN_ORDER)

but when i make

echo count($res) the result is 1, but the returning array give me 3 ty's

like i test:

$matches[0][0]."<br>";
$matches[0][1]."<br>";
$matches[0][2]."<br>";

Why is count function returning an incorrect number when the test works well?
echo count($res) is giving you 1, because it's a 2 dimensional array.
To see the actual number of results try:
echo count($res[0]);
Avatar of joyacv2

ASKER

I have to say that these solutions are excellent and works perfect for my implementation in my code!!!!

Thank You very much to Dan and Terry!
You're welcome and I'm glad I could help!