How to set file pointer of second preg_match

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
infomamun
Forum Contributor
Posts: 102
Joined: Mon Dec 28, 2009 7:48 pm

How to set file pointer of second preg_match

Post by infomamun »

Hi there,
Suppose I have the following $subject (as .txt file) from which I have to extract two lines of data:

[text] Final Index Change(Points) Change(%)
------------ -------------- -----------

ALL SHARES PRICE INDEX (DSI) 6881.38381 -235.73029 -3.3121611
DSE - 20 INDEX (DS20) 4924.70899 -198.65431 -3.8774199
DSE GENERAL INDEX (DGEN) 8295.41835 -284.77944 -3.3190311


All Category

ISSUES ADVANCED : 51
ISSUES DECLINED : 191
ISSUES UNCHANGED : 3
TOTAL ISSUES TRADED : 245


A Category (Equity)

ISSUES ADVANCED : 16
ISSUES DECLINED : 166
ISSUES UNCHANGED : 0
TOTAL ISSUES TRADED : 182


B Category (Equity)

ISSUES ADVANCED : 4
ISSUES DECLINED : 6
ISSUES UNCHANGED : 0
TOTAL ISSUES TRADED : 10[/text]

Suppose I want to extract the value from these two lines from the above subject:


[text]ISSUES ADVANCED : 4[/text]
[text]1st preg_match:
preg_match(/^ISSUES ADVANCEd\s+:\s+(\d+)/m, $subject)[/text]


[text] TOTAL ISSUES TRADED : 182[/text]
[text]2nd preg_match:
preg_match(/^TOTAL ISSUES TRADED\s+:\s+(\d+)/m, $subject)[/text]

As you can see I have to use two different preg_match in this case, the 1st preg_match will start searching it's pattern from the starting of the subject(the .txt file) and when pattern will be matched, it will stop searching. Next the 2nd preg_match will start searching again from the starting of the subjec(.txt) file. I want that the 2nd preg_match will start searching from the place where 1st preg_match would stopped after matching it's (1st) pattern. i.e I don't want that every preg_match will start searching from the beginning of the file.

How I can set the file pointer of 2nd preg_match at the end of 1st preg_match?
User avatar
ridgerunner
Forum Contributor
Posts: 214
Joined: Sun Jul 05, 2009 10:39 pm
Location: SLC, UT

Re: How to set file pointer of second preg_match

Post by ridgerunner »

For starters, neither of your regexes will match. In your subject text there are spaces before "ISSUES ADVANCED" and "TOTAL ISSUES TRADED". Your regex needs to allow for this so use '/^\s*ISSUES...' instead of '/^ISSUES...'.

Secondly, with PHP, it is somewhat tricky to have one regex pick up where the last one left off. It can be done, but it involves using the optional offset parameter passed to preg_match() in conjunction with the PREG_OFFSET_CAPTURE flag. See: PHP Manual - preg_match.

But instead of messing with the offsets, you can just use preg_match_all() and get all the matches in the file in one whack. You may also want to take a look at the preg_split() function if you need to first partition the file into chunks.
infomamun
Forum Contributor
Posts: 102
Joined: Mon Dec 28, 2009 7:48 pm

Re: How to set file pointer of second preg_match

Post by infomamun »

Hi ridgerunner
Very Happy to see you here again. In my last post, as you did not reply, so I have opened this post seperately.

But ridge, there is no space I have found in front of first letter, because I have already tested the regrex. Secondy although offset capture can be used for this but how preg_match_all? Because both patterns have same format, so I have to use exact word in both patterns instead \w+. And preg_match_all can use one pattern at a time but I need two patterns in this case.
User avatar
ridgerunner
Forum Contributor
Posts: 214
Joined: Sun Jul 05, 2009 10:39 pm
Location: SLC, UT

Re: How to set file pointer of second preg_match

Post by ridgerunner »

No, there are 4 spaces before each instance of "ISSUES ADVANCED" and "TOTAL ISSUES TRADED" in both your post above and in the original TXT file you posted. Your regex above does NOT match the subject text!

I'm afraid I don't have much more time to help you with this but I can offer one more suggestion. You can use the preg_match_all() and change the regex to allow two options or alternatives for the initial text like so:

Code: Select all

<?php
$re = '/^\s*(ISSUES ADVANCED|TOTAL ISSUES TRADED)\s+:\s+(\d+)/mi';
$data = file_get_contents('mst12-12-10.txt');
$record_count = preg_match_all($re, $data, $matches, PREG_SET_ORDER);
printf("Record matches count = %d\n", $record_count);
for ($i = 0; $i < $record_count; $i++) {
    printf("Match%3d of%3d: %-20s = %s\n",
    	$i + 1, $record_count, $matches[$i][1], $matches[$i][2]);
}
?>
Here is the output when you run this script on your original file: mst12-12-10.txt:

[text]Record matches count = 18
Match 1 of 18: ISSUES ADVANCED = 51
Match 2 of 18: TOTAL ISSUES TRADED = 245
Match 3 of 18: ISSUES ADVANCED = 16
Match 4 of 18: TOTAL ISSUES TRADED = 182
Match 5 of 18: ISSUES ADVANCED = 4
Match 6 of 18: TOTAL ISSUES TRADED = 10
Match 7 of 18: ISSUES ADVANCED = 0
Match 8 of 18: TOTAL ISSUES TRADED = 0
Match 9 of 18: ISSUES ADVANCED = 0
Match 10 of 18: TOTAL ISSUES TRADED = 6
Match 11 of 18: ISSUES ADVANCED = 4
Match 12 of 18: TOTAL ISSUES TRADED = 14
Match 13 of 18: ISSUES ADVANCED = 26
Match 14 of 18: TOTAL ISSUES TRADED = 31
Match 15 of 18: ISSUES ADVANCED = 1
Match 16 of 18: TOTAL ISSUES TRADED = 2
Match 17 of 18: ISSUES ADVANCED = 0
Match 18 of 18: TOTAL ISSUES TRADED = 0[/text]

:)
Post Reply