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
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 need a regular expression to find all the lines that begins with /*
$num_queries = preg_match_all(
'REG_EXP',
file_get_contents(__DIR__ . DIR_PLANTILLAS . '/' . 'temp_template.sql')
);
I try this '^\/\*.*' but it does not work.
If you use this string: /^\/\*.*/ in the preg_match() function, it'll work. This pattern matches /* followed by maybe some text.
Make sure the regular expression will be performed on each line. I recommend that you first split the string (file contents) by a newline. You can use the function preg_split() in order to do so.
If you don't want to split the file contents by each line first, then you can use the following pattern: /(^|\n)\/\*(.*)/. That pattern matches first either the beginning of the string or a newline, followed by /*, followed by maybe some text.
Notice that in the patterns /^\/\*.*/ and /(^|\n)\/\*(.*)/ the / is used as delimiter. That means that further occurences of / must be escaped.
Please, note, what you deal with multiline content, but ^ means beginning of content, not a beginning of a line.
try (\/[^\r\n]+)[\r\n$]+
Try this.
^\/\*+[^\n]*$
Edit: correction re escaping the /
I'm trying to build out a preg_match_all so I can use the array, but the problem is that I can't refine my regex to work.
An example input would be:
#|First Name|#
This text shouldn't be caught
#|Second Value|#
First Name and Second Value should both be in the array.
Here's what I've tried:
preg_match_all('/\#\|(.*?)\|\#\]/',$html, $out);
and
preg_match_all ("/^#\|*\|#*$/i", $html, $out);
The first is about right, it only contains \] near the end which breaks what you want. Using
/\#\|(.*?)\|\#/
does give me correct results, with the matches in $out[1].
Try this regexp :
/\#\|(.*?)\|\#/
Try with:
preg_match_all('/#\|(.*?)\|#/', $html, $out);
Escaping # should not be needed. To specify no # between #'s, you might also use
preg_match_all('/#\|([^#]*)\|#/', $html, $out);
a couple of things, you need to add the '.' character in between your bars. This character means match any character, without it you are saying match zero or more '|' characters. you also need to add the m modifier for multiline matching.
/^#\|.*\|#$/im
i have this regex string that i got from a website to pull emails from a file:
(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|"(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])*")#(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])+)\])
Ive tested it in regex buddy ( regex testing software ) and it works!
when i copy and paste the regex from regex buddy to my php file, i have to escape 2 " characters to make the regex form a valid string in php.
in php i use it like this:
$file = file_get_contents(/* URL TO GET */);
$email_pattern = "(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|\"(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])*\")#(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])+)\])";
$matches = array();
if ( preg_match_all ( $email_pattern, $file, $matches ))
{
echo print_r($matches, true);
}
but i get this warning!?!?
Warning: preg_match_all() [function.preg-match-all]: Unknown modifier '#'
however this regex works in regex buddy?
Where am i going wrong???
2 things:
step 1:
You need to put delimiters ( the / before and after the regex, so that you may add modifier ):
$email_pattern = "/(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|\"(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])*\")#(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])+)\])/";
step2:
And as your in a PHP string, you'll need to escape all the special character ( like \ that must become \\ , and $ that would become \$ , etc... )
So the escape to include the regex in a PHP String should look like this:
(?:[a-z0-9!#$%&\'*+/=?^_`{|}~-]+(?:\\.[a-z0-9!#$%&\'*+/=?^_`{|}~-]+)*|\\\"(?:[\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x21\\x23-\\x5b\\x5d-\\x7f]|\\\\[\\x01-\\x09\\x0b\\x0c\\x0e-\\x7f])*\\\")#(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x21-\\x5a\\x53-\\x7f]|\\\\[\\x01-\\x09\\x0b\\x0c\\x0e-\\x7f])+)\\])
And you also have to escape /, as we use that caracter for the delimiter of the first step. So we need the regex to see \/, but as we express the regex in a php string, we will replace / by \\/
If I'm right -- usually I use regex buddy too to do the conversion with the PHP export tool, but now I don't have it so I've done it by hand-- it should give something LIKE this:
$email_pattern = '/(?:[a-z0-9!#$%&\'*+\/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&\'*+\/=?^_`{|}~-]+)*|"(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\\\\[\x01-\x09\x0b\x0c\x0e-\x7f])*")#(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\\\\[\x01-\x09\x0b\x0c\x0e-\x7f])+)\])/';
I would also suggest that you put the string inside single quote.
I tried and...
Single quotes will give an error...
Use double quotes and the {} as delimiters // gives an error also
i'm trying to create a regular expression for this kind of log:
[9/23/11 11:09:53:632 CEST] 0000005b FormLoginExte 3 Form based login: userid/password present in the form. User is: user1
i've created this:
preg_match("/^[\d+/\d+/\d+\ \d+:\d+:\d+:\d+\ CEST]\ \w{8}\ \w+?\ +\w+?\ +.+?$", $line, $matches); // pattern to format the line
and get this error:
[9/21/11 12:11:03:126 CEST] 0000004d FormLoginExte 3 Form based login: userid/password present in the form. User is: user1
Warning: preg_match() [function.preg-match]: Unknown modifier '\' in C:\AppServ\www\logWebsphere\logWebspere.php on line 79
Regular expressions (preg_match) are slower than running a simple:
<?php
$data = explode(" ","[9/23/11 11:09:53:632 CEST] 0000005b FormLoginExte 3 Form based login: userid/password present in the form. User is: user1");
//data now contains all the info that you need
var_dump($data);
?>
POC here: http://codepad.org/nmo90QlI
You are using forward slashes as your delimeter but are also including unescaped forward slashes in your regex. You should escape them first by preceding them with a two backslashes. Two backslashes are required because the backslash is itself a meta character and must be escaped by using a second backslash. This in effect creates one backslash which can be used to escape the delimeter.
So your regex becomes:
preg_match("/^[\d+\\/\d+\\/\d+\ \d+:\d+:\d+:\d+\ CEST]\ \w{8}\ \w+?\ +\w+?\ +.+?$/", $line, $matches);
Please note you were also missing a delimiter.
Additionally, you could simply change delimters, like so:
preg_match("~^[\d+\/\d+\/\d+\ \d+:\d+:\d+:\d+\ CEST]\ \w{8}\ \w+?\ +\w+?\ +.+?$~", $line, $matches);
Edit
I've just noticed that you are trying to match [ and ] as literals. These, however, are meta characters (character classes) and should, I believe, also be escaped( \[ and \]).