regular expression that matches the below pattern - php

I'm not good in regular expression, but today I faced an unavoidable situation, So I need a regular expression that matches the case below:
|hhas.jpg||sd22-9393das.png||8jjas.png||IMG00338-20110109.jpg|
I tried this regex : /(?<=\|)(\w|\d+\.\w+)(?=\|)/i but not getting the desired results...
I want to match all the strings by using preg_match function of PHP between two | signs ie hhas.jpg, sd22-9393das.png etc...

You can use the following regex:
/\|([^|]*)\|/gi
Demo
Matched strings:
1. hhas.jpg
2. sd22-9393das.png
3. 8jjas.png
4. IMG00338-20110109.jpg

Use this..
preg_match_all('/\|(.*?)\||/', $str, $matches);
print_r(array_filter($matches[1]));
OUTPUT :
Array
(
[0] => hhas.jpg
[1] => sd22-9393das.png
[2] => 8jjas.png
[3] => IMG00338-20110109.jpg
)
Demonstration

your expression :
/(?<=\|)(\w|\d+\.\w+)(?=\|)/i
pretty well written , but just has a few minor flaws
when you say \w that is only one character.
the OR condition
\d+\.\w+ will match only when it meets the same order. i.e. list of digits first followed by a . and then followed by letters or digits or underscore.
better change your regex to :
/(?<=\|)(.*?)(?=\|)/ig
this will give you anything which is between |s
also IMHO , using lookarounds for such a problem is an overkill. Better use :
/\|(.*?)\|/ig

Try without using regular expression.
explode('||', rtrim(ltrim ('|hhas.jpg||sd22-9393das.png||8jjas.png||IMG00338-20110109.jpg|','|'),'|'));
Output:
Array ( [0] => hhas.jpg [1] => sd22-9393das.png [2] => 8jjas.png [3] => IMG00338-20110109.jpg )

Related

PHP : Matching strings between two strings

i have a problem with preg_match , i cant figure it out.
let the code say it :
function::wp_statistics_useronline::end
function::wp_statistics_visitor|today::end
function::wp_statistics_visitor|yesterday::end
function::wp_statistics_visitor|week::end
function::wp_statistics_visitor|month::end
function::wp_statistics_visitor|total::end
these are some string that run functions inside php;
when i use just one function::*::end it works just fine.
but when it contain more than one function , not working the way i want
it parse the match like :
function::wp_statistics_useronline::end function::wp_statistics_visitor|today::end AND ....::end
so basically i need Regex code that separate them and give me an array for each function::*::end
I assume you were actually using function::(.*)::end since function::*::end is never going to work (it can only match strings like "function::::::end").
The reason your regex failed with multiple matches on the same line is that the quantifier * is greedy by default, matching as many characters as possible. You need to make it lazy: function::(.*?)::end
It's pretty straight forward:
$result = preg_match_all('~function::(\S*)::end~m', $subject, $matches)
? $matches[1] : [];
Which gives:
Array
(
[0] => wp_statistics_useronline
[1] => wp_statistics_visitor|today
[2] => wp_statistics_visitor|yesterday
[3] => wp_statistics_visitor|week
[4] => wp_statistics_visitor|month
[5] => wp_statistics_visitor|total
)
And (for the second example):
Array
(
[0] => wp_statistics_useronline
[1] => wp_statistics_visitor|today
)
The regex in the example is a matching group around the part in the middle which does not contain whitespace. So \S* is a good fit.
As the matching group is the first one, you can retrieve it with $matches[1] as it's done after running the regular expression.
This is what you're looking for:
function\:\:(.*?)\:
Make sure you have the dot matches all identifier set.
After you get the matches, run it through a forloop and run an explode on "|", push it to an array and boom goes the dynamite, you've got what you're looking for.

PHP preg_match: comma separated decimals

This regex finds the right string, but only returns the first result. How do I make it search the rest of the text?
$text =",415.2109,520.33970,495.274100,482.3238,741.5634
655.3444,488.29980,741.5634";
preg_match("/[^,]+[\d+][.?][\d+]*/",$text,$data);
echo $data;
Follow up:
I'm pushing the initial expectations of this script, and I'm at the point where I'm pulling out more verbose data. Wasted many hours with this...can anyone shed some light?
heres my string:
155.101.153.123:simple:mass_mid:[479.0807,99.011, 100.876],mass_tol:[30],mass_mode: [1],adducts:[M+CH3OH+H],
130.216.138.250:simple:mass_mid:[290.13465,222.34566],mass_tol:[30],mass_mode:[1],adducts:[M+Na],
and heres my regex:
"/mass_mid:[((?:\d+)(?:.)(?:\d+)(?:,)*)/"
I'm really banging my head on this one! Can someone tell me how to exclude the line mass_mid:[ from the results, and keep the comma seperated values?
Use preg_match_all rather than preg_match
From the PHP Manual:
(`preg_match_all`) searches subject for all matches to the regular expression given in pattern and puts them in matches in the order specified by flags.
After the first match is found, the subsequent searches are continued on from end of the last match.
http://php.net/manual/en/function.preg-match-all.php
Don't use a regex. Use split to split apart your inputs on the commas.
Regexes are not a magic wand you wave at every problem that happens to involve strings.
Description
To extract a list of numeric values which may include a single decimal point, then you could use this regex
\d*\.?\d+
PHP Code Example:
<?php
$sourcestring=",415.2109,520.33970,495.274100,482.3238,741.5634
655.3444,488.29980,741.5634";
preg_match_all('/\d*\.?\d+/im',$sourcestring,$matches);
echo "<pre>".print_r($matches,true);
?>
yields matches
$matches Array:
(
[0] => Array
(
[0] => 415.2109
[1] => 520.33970
[2] => 495.274100
[3] => 482.3238
[4] => 741.5634
[5] => 655.3444
[6] => 488.29980
[7] => 741.5634
)
)

PHP Separate two different sections in one input

I'm working on a PHP based application extension that will extend a launcher style app via the TVRage API class to return results to a user wherever they may be. This is done via Alfred App (alfredapp.com).
I would like to add the ability to include show name followed by S##E##:
example: Mike & Molly S01E02
The show name can change, so I can't stop it there, but I want to separate the S##E## from the show name. This will allow me to use that information to continue the search via the API. Even better, if there was a way to grab the numbers, and only the numbers between the S and the E (in the example 01) and the numbers after E (in the example 02) that would be perfect.
I was thinking the best function is strpos but after looking closer that searches for a string within a string. I believe I would need to use a regex to correctly do this. That would leave me with preg_match. Which led me to:
$regex = ?;
preg_match( ,$input);
Problem is I just don't understand Regular Expressions well enough to write it. What regular expression could be used to separate the show name from the S##E## or get just the two separate numbers?
Also, if you have a good place to teach regular expressions, that would be fantastic.
Thanks!
You can turn it around and use strrpos to look for the last space in the string and then use substr to get two strings based on the position you found.
Example:
$your_input = trim($input); // make sure there are no spaces at the end (and the beginning)
$last_space_at = strrpos($your_input, " ");
$show = substr($your_input, 0, $last_space_at - 1);
$episode = substr($your_input, $last_space_at + 1);
Regex:
$text = 'Mike & Molly S01E02';
preg_match("/(.+)(S\d{2}E\d{2})/", $text, $output);
print_r($output);
Output:
Array
(
[0] => Mike & Molly S01E02
[1] => Mike & Molly
[2] => S01E02
)
If you want the digits separately:
$text = 'Mike & Molly S01E02';
preg_match("/(.+)S(\d{2})E(\d{2})/", $text, $output);
print_r($output);
Output:
Array
(
[0] => Mike & Molly S01E02
[1] => Mike & Molly
[2] => 01
[3] => 02
)
Explanation:
. --> Match every character
.+ --> Match every character one or more times
\d --> Match a digit
\d{2} --> Match 2 digits
The parenthesis are to group the results.
www.regular-expressions.info is a good place to learn regex.

Problem (un-)greedy RegExp

Consider the following Strings:
1: cccbbb
2: cccaaabbb
I would like to end up with are matches like this:
1: Array
(
[1] =>
[2] => bbb
)
2: Array
(
[1] => aaa
[2] => bbb
)
How can I match both in one RegExp?
Here's my try:
#(aaa)?(.*)$#
I have tried many variants of greedy and ungreedy modifications but it doesn't work out. As soon as I add the '?' everything is matched in [2]. Making [2] ungreedy doesn't help.
My RegExp works as expected if I omit the 'ccc', but I have to allow other characters at the beginning...
/(aaa)?((.)\3*)$/
There will be an extra [3] though. I don't think that's a problem.
Thanks for the brainstorming here guys! I have finally been able to figure something out that's working:
^(?:([^a]*)(aaa))?(.*)$
here's a non-regex way. search and split on "aaa" if found, then store the rest of the right side of "aaa" into array.
$str="cccaaabbb";
if (strpos($str,"aaa")!==FALSE){
$array[]="aaa";
$s = explode("aaa",$str);
$array[]=end($s);
}
print_r($array);
output
$ php test.php
Array
(
[0] => aaa
[1] => bbb
)
As for [1], depending on what's your criteria when "aaa" is not found, it can be as simple as getting the substring from character 4 onwards using strpos().
this will match the groups but its not very flexible can you put a little more detail of what you need to do. It may be much easier to grab three characters a time and evaluate them.
Also I tested this in poweshell which has a slightly different flavor of regex.
(a{3,3})*(b{3,3})
do like this:
$sPattern = "/(aaa?|)(bbb)/";
this works well.

Can preg_match() (or other php regex function) match a variable number of parenthesized subpatterns?

Suppose I have '/srv/www/site.com/htdocs/system/application/views/' and want to test it against a regexp that matches each directory name in the path?
Something like this pattern: '(/[^/])'
That yields an array with 'srv','www','site.com'... etc.
PS: the regexp syntax I wrote is just to illustrate, it's not tested and surely wrong, but just to give an idea.
PS2: I know there's explode() but let's see if we can do this with a regexp (it's useful for other languages and frameworks which don't have explode).
preg_match_all:
$str = '/srv/www/site.com/htdocs/system/application/views/';
preg_match_all('/\/([^\/]+)/', $str, $matches);
// $matches[0] contains matching strings
// $matches[1] contains first subgroup matches
print_r($matches[1]);
Output:
Array
(
[0] => srv
[1] => www
[2] => site.com
[3] => htdocs
[4] => system
[5] => application
[6] => views
)
There is preg_split for splitting files on regular expressions similar to explode, and then there is preg_match_all which does what you want.
I don't think you can, but you could instead use preg_match_all() to get multiple matches from a regular expression. There is also preg_split() which may be more appropriate.

Categories