1

I'm reading html source code and I need to get below data from it.

LMS.pageData['product']['maxThreshold'] ='6';

in above example I need to catch 6.

1
  • Does the provided example represent a fixed pattern? Commented May 8, 2015 at 11:21

2 Answers 2

1

You can use a simple capture grouping :

"LMS.pageData\['product'\]\['maxThreshold'\] ='(\d)';"

then you need to extract the 1st group from matched pattern!

Demo

You can also use look-around that returns the number directly without need to get the first group :

"(?<=LMS.pageData\['product'\]\['maxThreshold'\] =')\d(?=';)"
Sign up to request clarification or add additional context in comments.

2 Comments

I'm a beginner, kindly give me a full sample code in PHP
@HamidJoukar SO is not a code writing service! you can simply search about the grubbing the groups from a match regex in preg!
1

You can use the look-arounds to get a number inside '...' at the end of the line, like this:

<?php
$re = "#(?<=')\d+(?=';\s*$)#m"; 
$str = "LMS.pageData['product']['maxThreshold'] ='6';"; 
preg_match($re, $str, $matches);
print_r($matches[0]);

See IDEONE demo

Use preg_match_all to obtain multiple matches if input contains multiple lines with '6';s.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.