I have used the online regex tester http://gskinner.com/RegExr/ to come up with the following pattern: ( I've pasted all three lines as I'm not sure if i should be pasting the Regexp or the pattern.)
RegExp: /<div class="label">.*?<h3>(.*?)</h3>.*?"more">(.*?)\|/g
pattern: <div class="label">.*?<h3>(.*?)</h3>.*?"more">(.*?)\|
flags: g
If I use it in php like this:
$pattern = '/<div class="label">.*?<h3>(.*?)</h3>.*?"more">(.*?)\|/g';
preg_match_all($pattern,$page,$matches );
I get an error:
Warning: preg_match_all()
[function.preg-match-all]: Unknown
modifier '.' in ...
Can someone please explain how I can get my regex from this tool, into the correct format for use in PHP. Many thanks.
You're not escaping the slash in </h3> and g means apply globally, that's not needed here since you're using preg_match_all, and it's not a valid modifier in PHP's implementation of regex, just omit it
Try this:
$pattern = '/<div class="label">.*?<h3>(.*?)<\/h3>.*?"more">(.*?)\|/';
Related
I'm trying to check if a string ends with one _ and two letters on an old system with php. I've check here on stackoverflow for answers and I found one that wanted to do the same but with one . and two digits.
I tried to change it to work with my needs, and I got this:
\\.*\\_\\a{2,2}$
Then I went to php and tried this:
$regex = '(\\.*\\_\\a{2,2}$)';
echo preg_match($regex, $key);
But this always returns an error, saying the following:
preg_match(): Delimiter must not be alphanumeric or backslash
I get this happens because I can't use the backslashes or something, how can I do this correctly? And also, is my regex correct(I don't know ho to form this expressions and how they work)?
You can use this regex with delimiters:
$regex = '/_[a-z]{2}$/i';
You're getting that error because in PHP every regex needs a delimiter (not use of / above which can be any other character like ~ also).
^.*_[a-zA-Z]{2}$
This should do it for you.
$re = "/^.*_[a-zA-Z]{2}$/";
$str = "abc_ac";
preg_match($re, $str);
I cannot get the following regex to work properly:
preg_match('Currently: ([0-9\.km,]+)', $data, $matches)
The information inside of $data is: 'What it is Currently: 52,523' (along with about 30 lines of html).
I keep getting the following error: Warning: preg_match(): Delimiter must not be alphanumeric or backslash in C:\xampp\htdocs\test\test.php on line 49
Note: Line 49 contains the preg_match that I posted above.
Edit: Apologies, i forgot to add in the matches parameter.
the preg_match function requires a delimiter in your pattern.
preg_match('/Currently: ([0-9\.km,]+)/', $data, $matches)
edit:
as described in the comments
#JoséRobertoAraújoJúnior curl_setopt($curl, CURLOPT_RETURNTRANSFER,
1); If that is what you mean, yes I have set it. If i echo $data, it
displays the webpage, which means it still contains all the html
tags.. I'm not sure what to do from here, to use that data in my preg_
Then it's possible that $data contains white-spaces between: Currently: and (...), so this should be considered in your pattern by adding \s+ instead of a normal white-space, \s will match any white space character as described in the PCRE regex syntax documentation page.
preg_match('/Currently:\s+([0-9\.km,]+)/', $data, $matches)
Live test:
http://codepad.viper-7.com/xJNisE
you need to add the same character at the begining and end of the pattern:
preg_match('/Currently: ([0-9\.km,]+)/', $data)
This character can't appear inside the pattern unless you escape it, for example:
preg_match('/<example><\/example>/', $xml)
You can use other characters as delimiters, one of the most used beside / is #
You have to use delimiter like this edit
$data = 'What it is Currently: 52,523';
preg_match('&Currently: ([0-9\.km,]+)&', $data, $match);
print_r($match);
working example http://codepad.viper-7.com/QGOoXT
I wrote a regex in RegExr to tackle the following string:
<?php _on*/4353452f43f43f46 xx46 _off*/ ?>
This is the Regex code:
(.*<?php.*)(.*_on.*)(.*_off.*)(.*?>)
Which is working fine here:
http://regexr.com?31ptt
But it doesn't work with PHP, I get weird errors like: "Unknown modifier '<'", etc.
What do I need to do to convert this to work with PHP?
This is my php code:
$virusstring = '(.*/<?php.*)(.*_on.*)(.*_off.*)(.*?>)';
if(preg_match($virusstring,$myfile)) {
$fixed = preg_replace($virusstring,'',$myfile);
$blah = file_put_contents($item, $fixed);
}
$myfile is just taken from the infected file that is being scanned.
Your regular expression is missing delimiters. You need to add delimiters or PHP will assume your opening ( is a delimiter:
/(.*<\?php.*)(.*_on.*)(.*_off.*)(.*\?>)/
Also, ? is a quantifier, matching 0 or 1 of the previous character. You need to escape it:
(.*<\?php.*)(.*_on.*)(.*_off.*)(.*\?>)
This seems to work fine
preg_match("/(.*<\\?php.*)(.*_on.*)(.*_off.*)(.*\\?>)/us", $searchText)
I am having some trouble with regex in php (preg_match_all).
I am using the following code to find an email encapsulated by <>:, i.e. :
preg_match_all("<[_a-z0-9-]+(\.[_a-z0-9-]+)*#[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})>:", $body,$matches);
For some reason PHP is blowing up at the colon with the following error:
Warning: preg_match_all() [function.preg-match-all]: Unknown modifier ':' in...
Any help would be much appreciated, as I am no regex guru, and am just about out of hair to pull.
You need to use delimeters EX:
preg_match_all('/<[_a-z0-9-]+(\.[_a-z0-9-]+)*#[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})>:/', $body,$matches);
See the / I added on both ends telling PHP where the regex starts and ends.
You could use T-Regx which doesn't need / at the start and end
$pattern = "<[_a-z0-9-]+(\.[_a-z0-9-]+)*#[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})>:";
$matches = Pattern::of($pattern)->match($body)->all();
I've been trying to use grubers latest url matching regex in a php project.
To test it I threw together something very simple:
$regex = "(?i)\b((?:[a-z][\w-]+:(?:/{1,3}|[a-z0-9%])|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:"'.,<>?«»“”‘’]))";
$array = pret_match_all($regex, $theblockofurltext);
print_r($array);
The first problem was the " would escape a string, depending which I wrapped the regex with, so I just removed it. The use of this is personal and I will never have " anywhere near a url anyway. This left me with a new regex.
$regex = "(?i)\b((?:[a-z][\w-]+:(?:/{1,3}|[a-z0-9%])|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:'.,<>?«»“”‘’]))";
Raring to go I then ran my little script and it gave me the following error:
Warning: preg_split() [function.preg-split]: Unknown modifier '\' in D:\wwwroot\xxx\index.php on line 14
Unfortunately my REGEX class at school wasn't taught to anywhere near the levels of this regex requires, and I have no idea where to begin fixing this for use with PHP. Any help would be greatly appreciated. No doubt I'm probably doing something stupid too, so please go easy on me :)
Jon
Add # before and after your RE.
$regex = "#(?i)\b((?:[a-z][\w-]+:(?:/{1,3}|[a-z0-9%])|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:'.,<>?«»“”‘’]))#";
If you use PCRE, the regular expression must be enclosed in delimiters. Now, parenthesis () can also be delimiters, that is why the engine thinks, your expression is only (?i) and interprets the next \ as modifier.
You could use ~ as delimiter:
$regex = "~(?i)\b...]))~";
Update:
I don't know whether PHP supports the partial modifying of an expression with (?i). So you might have to remove this and put the modifier after the delimiter instead (you apply it to the whole expression anyway):
$regex = "~\b...]))~i";