I need some help creating regular expressions to pick information out of a file. I am using php preg_match to do it and am trying to get information that looks like the following:
ex.
19-Aug-2013 //The date will always be in the format.
and a number like this
303.00
The file I am trying to get this information from is the body of a mime type email.
I only need these two types of specific information.
Try this as a whole:
preg_match_all('/(\d{2}-\w{3}-\d{4}|\d+\.\d{1,2})/', $file, $matches);
\d{2}-\w{3}-\d{4}: Get the date
\d+\.\d{1,2}: Get the float
By using preg_match_all you return all found matches in all the haystack. preg_match only matches the first occurance.
For the date you can use:
[0-9]{2}-[a-z]{3}-[0-9]{4}
For the number:
[0-9]+\.[0-9]{2}
With no specific input is hard to come up with a better regex at the moment...
Related
I'm new to php and I'm trying to write a function to find an invalid postcode. This is an option, however I've been told this isnt the ideal format:
function postcode_valid($postcode) {
return preg_match('/\w{2,3} \d\w{2}/', $postcode);
}
//more accurate
//[A-Z]{1,2}[0-9]{1,2}[A-Z]? [0-9][A-Z]{2}
I understand the first function, but I don't know how to write the 'ideal' solution as a function, please can you advise?
If the regular expression you provided in the comment field is the correct one and you don't know how to use it in PHP, here is the solution:
function postcode_valid($postcode) {
return preg_match('/^[A-Z]{1,2}[0-9]{1,2}[A-Z]? [0-9][A-Z]{2}$/', $postcode);
}
You need to add two slashes (one in front, one at the end) of the regular expression and pack it in a string in PHP. I would also highly recommend you to use ^ and $ at the beginning resp. at the end of the regular expression to indicate the beginning and the end of the string (otherwise, it is valid, if only a part of the string contains the correct pattern i.e. a longer string with a valid part would be accepted.) Here is a live example.
If you are looking for the validation of a UK post code, you should be using the following regex instead (source):
(GIR 0AA)|((([A-Z-[QVX]][0-9][0-9]?)|(([A-Z-[QVX]][A-Z-[IJZ]][0-9][0-9]?)|(([A-Z-[QVX]][0-9][A-HJKPSTUW])|([A-Z-[QVX]][A-Z-[IJZ]][0-9][ABEHMNPRVWXY])))) [0-9][A-Z-[CIKMOV]]{2})
If you are looking for something else, please provide a comment below.
I need to extract p5925 and c98 of the following URL:
http://www.example.com/mens-shoes/mens-boots/p5925/c98/colour/Black
The format I want is:
p:5925, c:98
The regex I got now is ([^pc]\d+) which matches just the numbers but not sure how I can get it in the format I wanted.
I used the code preg_match('/([^pc]\d+)/', $ls_url, $la_matches); to split it.
You need to use preg_match_all
preg_match_all('~(?<=/)([pc])(\d+)(?=/)~', $ls_url, $la_matches);
or
preg_match_all('~\b([pc])(\d+)\b~', $ls_url, $la_matches);
DEMO
Extracting Strings on Multiline
Please share your ideas on this concern.
how do i extract the string Domain/username field on this multi threaded line.
Name Username
COMP01-100 Domain\user.one
Name Username
COMP02-100 Domain\test.user
Name Username
COMP10-100 Domain\sample.user
--
Note:
I should have searched for COMP02-100 and extract Domain\test.user as result.
I think I should have used the php preg_match_all function but i just can't find the right syntax on this.
Any ideas?
If I understand you correctly, you could get the desired result with these lines:
$search_string= 'COMP02-100';
preg_match("/$search_string (\w+\\\\\w+.\w+)/", $multiline, $matches);
preg_match is sufficient if you just want the first match
Ok regex experts. I'm having a ton of trouble trying to make a regex pattern for my needs.
The goal:
Take a search query such as "good food type:post format:gallery" and parse the type or format or both from the string.
This is what I wrote, but doesnt work unless both type and format are present and type comes before format. Ideally, either type or format could be present.
$query = "Great food type:post format:gallery";
preg_match('/(.*?(?<=\btype:)(?P<type>[a-z]*\w+))(.*?(?<=\bformat:)(?P<format>[a-z]*\w+))/', $query, $matches);
I image I need the returned $matches to be named as well right?
Thanks,
I don't think you'll want to use a regex for this. It'll be a pain to maintain and update when you add more operators like type: and format: Also the regex then depends on ordering of what's entered.
A simple approach might be like
$tokens=explode(" ",$searchString);
foreach($tokens as $token){
if(preg_match('~([^:]+:(.*)~',$token,$flagMatch)){
$flags[$flagMatch[1]]=$flagMatch[2];
}
$searchtokens[]=$token
}
Obvious caveat with that example is exploding straight on space so you wouldn't be able to handle "quoted terms" that should be treated as one.
I have a pure text file without any HTML formatting. I want to search for a word in it? how should i do that? and I also want the next words before a comma. can i do that to?
so means:
if its: "word1":"i like stackoverflow", next thing
i want to find word1 which is in inverted commas and then i want the whole phrase i like stackoverflow without the inverted commas. and it should stop at that point.
is there any way to do that?
thanks...
Use a regular expression. In PHP, you can use a function like preg_match to do this.
For the pattern you described, your regular expression code could be something like:
$match_array = array();
$string_to_match = // load the string from a file and put it in this variable
preg_match('/"([A-Za-z0-9\s]+?)":"([A-Za-z0-9\s]+?)"/', $string_to_match, $match_array);
The results of the match will then be placed in $match_array.