Categories

See More
Popular Forum

MBA (4887) B.Tech (1769) Engineering (1486) Class 12 (1030) Study Abroad (1004) Computer Science and Engineering (988) Business Management Studies (865) BBA (846) Diploma (746) CAT (651) B.Com (648) B.Sc (643) JEE Mains (618) Mechanical Engineering (574) Exam (525) India (462) Career (452) All Time Q&A (439) Mass Communication (427) BCA (417) Science (384) Computers & IT (Non-Engg) (383) Medicine & Health Sciences (381) Hotel Management (373) Civil Engineering (353) MCA (349) Tuteehub Top Questions (348) Distance (340) Colleges in India (334)
See More

Converting Perl Regex to PHP [closed]

General Tech Bugs & Fixes
Max. 2000 characters
Replies

usr_profile.png
Bhanu Gupta

User

( 6 months ago )

I have the following Regex in PERL which I need to convert to PHP

if($line =~ m/[<]div class="row.* id="(vm_[0-9]{8,9})"[>]/i){ }

I currently have the following in PHP to match it, but I'm not getting the same results:

preg_grep('/[<]div class="row(.*) id="(vm_[0-9]{8,9})"[>]/', $lines);

Can anyone tell me if what I have is the same or if I'm mistaken?

usr_profile.png
Peter Jarvis

User

( 6 months ago )

preg_grep returns an array whereas if you use preg_match, you get a boolean. So if you are using:

if($line =~ m/[<]div class="row.* id="(vm_[0-9]{8,9})"[>]/i){ }

It would be an equivalent for:

if(preg_match('/[<]div class="row(.*) id="(vm_[0-9]{8,9})"[>]/i', $line)) { };

Though you can simplify the regex a bit:

if(preg_match('/<div class="row(.*) id="(vm_[0-9]{8,9})">/i', $line)) { };

If you want to see the matches, you could do:

if(preg_match('/<div class="row(.*) id="(vm_[0-9]{8,9})">/i', $line, $match)) {
    print_r($match);
};

usr_profile.png
Peter Jarvis

User

( 6 months ago )

preg_grep returns an array whereas if you use preg_match, you get a boolean. So if you are using:

if($line =~ m/[<]div class="row.* id="(vm_[0-9]{8,9})"[>]/i){ }

It would be an equivalent for:

if(preg_match('/[<]div class="row(.*) id="(vm_[0-9]{8,9})"[>]/i', $line)) { };

Though you can simplify the regex a bit:

if(preg_match('/<div class="row(.*) id="(vm_[0-9]{8,9})">/i', $line)) { };

If you want to see the matches, you could do:

if(preg_match('/<div class="row(.*) id="(vm_[0-9]{8,9})">/i', $line, $match)) {
    print_r($match);
};

usr_profile.png
Peter Jarvis

User

( 6 months ago )

preg_grep returns an array whereas if you use preg_match, you get a boolean. So if you are using:

if($line =~ m/[<]div class="row.* id="(vm_[0-9]{8,9})"[>]/i){ }

It would be an equivalent for:

if(preg_match('/[<]div class="row(.*) id="(vm_[0-9]{8,9})"[>]/i', $line)) { };

Though you can simplify the regex a bit:

if(preg_match('/<div class="row(.*) id="(vm_[0-9]{8,9})">/i', $line)) { };

If you want to see the matches, you could do:

if(preg_match('/<div class="row(.*) id="(vm_[0-9]{8,9})">/i', $line, $match)) {
    print_r($match);
};

what's your interest


forum_ban8_5d8c5fd7cf6f7.gif