regex websphere file with php - php

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 \]).

Related

Regular Expression to check if string ends with one underscore and two letters with php

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);

Unexpected ] error in simple preg replace script [duplicate]

This question already has answers here:
preg_match() Unknown modifier '[' help
(2 answers)
Closed 8 years ago.
I have a script that downloads the latest newsletter from a group inbox on a spare touchscreen in our office. It works fine, but people keep accidentally unsubscribing us so I want to hide the unsubscribe link from the email.
$preg_replace seems like it would work because I can set up a pattern that simply removes any link withthe word "unsubscribe" in. I validated the pattern below using the tool at http://regex101.com/ , and it even picks up variations like "manage subscription" as well. It is ok if the odd legitimate link with the word subscribe also get removed - there won't be many and it's only for internal use.
However, when I execute I get an error.
Here's my code:
line 53: $pat='<\s*(a|A)\s+[^>]*>[^<>]*ubscri[^<>]*<\s*\/(a|A)\s*>';
line 54: $themail[bodycontent]= preg_replace($pat, ' ',$themail[bodycontent]);
and I get this error:
preg_replace() [function.preg-replace]: Unknown modifier ']' in /home/trev/public_html/bigscreen/screen-functions.php on line 54
It must be something really simple like an unescaped char but I have gone code blind and can't for the life of me see it.
How do I get this pattern:
<\s*(a|A)\s+[^>]*>[^<>]*ubscri[^<>]*<\s*\/(a|A)\s*>
to run in a simple php script?
Thanks
You haven't used any delimiters so it's treating the < character as the delimiter
Try something like this instead
$pat='#<\s*(a|A)\s+[^>]*>[^<>]*ubscri[^<>]*<\s*\/(a|A)\s*>#';
You have no delimiter. Or rather you do, but it's not the one you meant. PCRE is interpreting your first < as the opening delimiter (you can use matching brackets as delimiters - in fact, I use parentheses to help remind myself that the entire match is index 0). Then it sees the first > as the ending delimiter. Anything after that should be a modifier, but of course ] is not a modifier.
Wrap your regex with (...) to give it a proper set of delimiters.
$themail[bodycontent] should be either $themail['bodycontent'] or $themail[$bodycontent].
It's trying to parse bodycontent] ... as the array index.
Patterns used in preg_match need to be enclosed by a pair of delimiter characters.
For example, a / or a ~ at the start and end of the string.
Anything outside of these delimiters at the end of the string is considered to be a regex "modifier".
Your example doesn't have delimiters, so PHP is wrongly assuming that the < character is the delimiter. It therefore sees the next < character as the closing delimiter, and therefore, anything after that as a modifier. Obviously all that stuff is supposed to be inside the pattern and isn't valid as modifiers, which is why PHP is complaining.
Solution: Add a pair of modifier characters:
$pat='~<\s*(a|A)\s+[^>]*>[^<>]*ubscri[^<>]*<\s*\/(a|A)\s*>~';
^ ^
add this ...and this
(it doesn't have to be ~, you can choose your own modifier character to suit your needs. Best one to use is one that doesn't occur in your string (although you can escape it if it does)
Starting and ending of pattern with slash /
$pat='/<\s*(a|A)\s+[^>]*>[^<>]*ubscri[^<>]*<\s*\/(a|A)\s*>/';

Regex preg_match() issues

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

Trying To Find Forward Slash In Preg_Match

I've been searching for hours trying to find a solution to this. I am trying to determing if the REQUEST URI is legit and break it down from there.
$samplerequesturi = "/variable/12345678910";
To determine if it is legit, the first section variable is only letters and is variable in length. The second section is numbers, which should have 11 total. My problem is escaping the forward slash so it is matched in the uri. I've tried:
preg_match("/^[\/]{1}[a-z][\/]{1}[0-9]{11}+$/", $samplerequesturi)
preg_match("/^[\\/]{1}[a-z][\\/]{1}[0-9]{11}+$/", $samplerequesturi)
preg_match("/^#/#{1}[a-z]#/#{1}[0-9]{11}+$/", $samplerequesturi)
preg_match("/^|/|{1}[a-z]|/|{1}[0-9]{11}+$/", $samplerequesturi)
Among others which I can't remember now.
The request usually errors out:
preg_match(): Unknown modifier '|'
preg_match(): Unknown modifier '#'
preg_match(): Unknown modifier '['
Edit:
I guess I should state that the REQUEST URI is already known. I'm trying to prove the whole string to make sure it isn't a bogus string ie to make sure there the 1st set is only lower case letters, and the 2nd set is only 11 numbers.
/ is not the only thing you can use as a delimiter. In fact, you can use almost any non-slphanumeric character. Personally I like to use () because it reminds me that the first item of the result array is the entire match and it also never needs escaping in the pattern.
preg_match("(^/([a-z]+)/(\d+)$)i",$samplerequesturi,$out);
var_dump($out);
That should do it.
If you want to use regex (which I don't think is necessary in this case, simply splitting on "/" should be fine:
$samplerequesturi = "/variable/12345678910";
preg_match("#^/([A-Za-z]+)/(\d+)$#", $samplerequesturi, $out);
echo $out[1];
echo $out[2];
should get you going
Your problem may be that you are using the / forward-slash as a regex delimiter (at the start and end of the regex expression). Switch to using a character other than the forward-slash, such as a # hash symbol or any other symbol which will never need to appear in this particular expression. Then you won't need to escape the forward-slash character at all in the expression.

how to use long regex strings in php

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

Categories