Explode values in a word/string with Regex - php

i have this sample string and i want to explode the day,month,hour,minutes,(am/pm) wih regex:
SunDec 16 00:00am
Using this:
(\w+)\s+(\w+)\s+(\d+)\:(\d+)(..)
But it gives me:
Array
(
[0] => SunDec 16 00:00am
[1] => SunDec
[2] => 16
[3] => 00
[4] => 00
[5] => am
)
I cant figure it out..Can i explode SubDec into two?

You could try this:
$str = 'SunDec 16 00:00am';
preg_match('/([A-Z]{1}\w+)([A-Z]{1}\w+)\s+(\w+)\s+(\d+):(\d+)(..)/', $str, $ret);
print_r($ret);

You can try this Regular Expression, This might help you
([A-Z][a-z]+)+\s(\d+)?\s?(\d+)\:(\d+)([a-z]+)

If your string is always going to follow that example you can use this:
$string = 'SunDec 16 00:00am';
$pattern = '!([a-zA-Z]{3})([a-zA-Z]{3})\s+(\w+)\s+(\d+)\:(\d+)(..)!';
preg_match($pattern, $string, $chunks);
print_r($chunks);
Which outputs:
Array
(
[0] => SunDec 16 00:00am
[1] => Sun
[2] => Dec
[3] => 16
[4] => 00
[5] => 00
[6] => am
)
You could use this pattern too which is slightly smaller
'!([a-z]{3})([a-z]{3})\s+(\w+)\s+(\d+)\:(\d+)(..)!i'

Related

PHP preg_match() doesn't match all subpatterns

I have a preg_match() which matches the pattern but doesn't receive the expected matches (in third param).
My regex patterns have multiple subpatterns.
$pattern = "~^&multi&[^&]+(&(?:(p-(?<sad>[1-9]\d*)|page-(?<sad>[1-9]\d*))))?&[^&]+(&(?:(p-(?<gogosi>[1-9]\d*)|page-(?<gogosi>[1-9]\d*))))?&?$~J";
$string = "&multi&mickael&p-23&george&page-34";
preg_match($pattern, $string, $matches);
This is what $matches contains:
Array
(
[0] => &multi&mickael&p-23&george&page-34
[1] => &p-23
[2] => p-23
[sad] =>
[3] => 23
[4] =>
[5] => &page-34
[6] => page-34
[gogosi] => 34
[7] =>
[8] => 34
)
The problem is [sad] should have 23 value.
If I don't include in $string second page (page-34), 'cause is optional [...]
$string = "&multi&mickael&p-23&george";
[...] I have good $matches 'cause my [sad] got his value:
Array
(
[0] => &multi&mickael&p-23&george
[1] => &p-23
[2] => p-23
[sad] => 23
[3] => 23
)
But I want regex to return properly value even when I have both paginations in $string.
What to do such that all subpatterns will have their value ?
Note: Words as ('p', 'page') are only examples. Can be any words there.
Note: Above data is just an example. Don't give me workaround solutions, but something good for any input data.
You may use a branch reset group, (?|...|...):
'~^&multi&[^&]+(&((?|p-(?<sad>[1-9]\d*)|page-(?<sad>[1-9]\d*))))?&[^&]+(&((?|p-(?<gogosi>[1-9]\d*)|page-(?<gogosi>[1-9]\d*))))?&?$~J'
See the regex demo.
See the PHP demo:
$pattern = "~^&multi&[^&]+(&((?|p-(?<sad>[1-9]\d*)|page-(?<sad>[1-9]\d*))))?&[^&]+(&((?|p-(?<gogosi>[1-9]\d*)|page-(?<gogosi>[1-9]\d*))))?&?$~J";
$string = "&multi&mickael&p-23&george&page-34";
if (preg_match($pattern, $string, $matches)) {
print_r($matches);
}
Output:
Array
(
[0] => &multi&mickael&p-23&george&page-34
[1] => &p-23
[2] => p-23
[sad] => 23
[3] => 23
[4] => &page-34
[5] => page-34
[gogosi] => 34
[6] => 34
)

Reg Exp - preg_match_all reduce array result

This is my Reg Exp "[c]?[\d+|\D+]\s*". My input is this "c7=c4/c5*100" and the result is :
Array
(
[0] => Array
(
[0] => c7
[1] => =
[2] => c5
[3] => +
[4] => c3
[5] => *
[6] => 1
[7] => 0
[8] => 0
)
)
But what I want is:
Array
(
[0] => Array
(
[0] => c7
[1] => =
[2] => c5
[3] => +
[4] => c3
[5] => *
[6] => 100
)
)
I can't seem to get the last part working, I'm lost as what to do next - Can anyone help?
Thanks,
Paul
You specified a character class [\d+|\D+] which would match any of the specified characters. I think you meant using an or | with a grouping construct c?(?:\d+|\D+)\s* but in that case it would match c followed by either \d+ or \D so that would match the = sign right after it resulting in c= as a match and /c as a match.
Try matching an optional c c? followed by one or more digits or | match not a digit \D
c?\d+|\D
$re = '/c?\d+|\D/m';
$str = 'c7=c4/c5*100';
preg_match_all($re, $str, $matches);
print_r($matches);
That will result in:
Array
(
[0] => Array
(
[0] => c7
[1] => =
[2] => c4
[3] => /
[4] => c5
[5] => *
[6] => 100
)
)
Demo

split string and make array, regular expression

$content = "[2][6][11]";
This i would like to split into an array with values [2], [6] and [11].
preg_split("/\[*\]/i", $content);
Wrong output: Array ( [0] => [2 [1] => [5 [2] => )
Any help what's wrong on the regular expression.
thanks.
You can use lookarounds for this split:
$content = "[2][6][11]";
print_r(preg_split('/(?<=\])(?=\[)/', $content));
Output:
Array
(
[0] => [2]
[1] => [6]
[2] => [11]
)
You can use lookarounds to test what are the characters around the position you want to find without matching them.
print_r(preg_split('~(?<=])(?=\[)~', $content));
Note that if you already know how your string is formatted, you can also use preg_match_all with a more simple pattern: ~\[\d+]~
You can also do it with preg_match_all :
$content = "[2][6][11]";
preg_match_all("/\[.*\]/Ui", $content, $matches);
$result = $matches[0];
print_r($result);
Output:
Array
(
[0] => [2]
[1] => [6]
[2] => [11]
)

get all words from string with regular expression

I need preg_match that can find me all words from string.
for example:
$str = "string: hi, it is string.";
I would like get this:
[0] => string
[1] => hi
[2] => it
[3] => is
[4] => string
I use with '/[a-z]+/ui', but I get this:
[0] => string:
[1] => hi,
[2] => it
[3] => is
[4] => string.
You said preg_match(), instead you should be using preg_match_all() and there is no need to use the u modifier in your regular expression here.
$str = "string: hi, it is string.";
preg_match_all('/[a-z]+/i', $str, $matches);
print_r($matches[0]);
Output
Array
(
[0] => string
[1] => hi
[2] => it
[3] => is
[4] => string
)

a question about preg_match

I have a question in PHP:
When using preg_match, why #^(([a-z]{2})/)?(([a-z\-]{3,})/(([a-z\-]{3,}))?)?$#i match ab/cde/fgh and do not match ab/cde?
(I mean:
preg_match_all('#^(([a-z]{2})/)?(([a-z\-]{3,})/(([a-z\-]{3,}))?)?$#i','ab/cde/fgh',$match)
$match = Array
(
[0] => ab/cde/fgd
[1] => ab/
[2] => ab
[3] => cde/fgd
[4] => cde
[5] => fgd
[6] => fgd
)
and
preg_match_all('#^(([a-z]{2})/)?(([a-z\-]{3,})/(([a-z\-]{3,}))?)?$#i','ab/cde',$match)
$match = Array ()
Because as the regex is written, you need a slash after the cde. ab/cde/ should match.
[a-z-]{3,} = 3 or more characters

Categories