Regex: Using capture data further in the regex - php

I want to parse some text that start with ":" and could be surround with parentheses to stop the match so:
"abcd:(someText)efgh" and
"abcd:someText"
will return someText.
but i have a problem to set the parentheses optionnal.
I make this but it does not works:
$reg = '#:([\\(]){0,1}([a-z]+)$1#i';
$v = 'abc:(someText)def';
var_dump(preg_match($reg,$v,$matches));
var_dump($matches);
The $1 makes it failed.
i don't know how to tell him :
If there is a "(" at the beginning, there must be ")" at the end.

You can't test if the count of something is equal to another count. It's a regex problem who can only be used with regular language (http://en.wikipedia.org/wiki/Regular_language). To achieve your goal, as you asked - and that is if there's a '(' should be a ')' -, you'll need a Context-Free Language (http://en.wikipedia.org/wiki/Context-free_language).
Anyway, you can use this regex:
'/:(\([a-z]+\)|[a-z]+)/i

To return the match of different sub-patterns in the regex to the same element of the $matches array, you can use named subpattern with the internal option J to allow duplicate names. The return element in $matches is the same as the name of the pattern:
$pattern = '~(?J:.+:\((?<text>[^)]+)\).*|.+:(?<text>.+))~';
$texts = array(
'abc:(someText)def',
'abc:someText'
);
foreach($texts as $text)
{
preg_match($pattern, $text, $matches);
echo $text, ' -> ', $matches['text'], '<br>';
}
Result:
abc:(someText)def -> someText
abc:someText -> someText
Demo

This regex will match either :word or :(word) groups 1 and 2 hold the respective results.
if (preg_match('/:([a-z]+)|\(([a-z]+)\)/i', $subject, $regs)) {
$result = ($regs[1])?$regs[1]:$regs[2];
} else {
$result = "";
}

regex: with look-behind
"(?<=:\(|:)[^()]+"
test with grep:
kent$ echo "abcd:(someText)efgh
dquote> abcd:someOtherText"|grep -Po "(?<=:\(|:)[^()]+"
someText
someOtherText

Try this
.+:\((.+)\).*|.+:(.+)
if $1 is empty there are no parentheses and $2 has your text.

Related

Why is my regex rejecting apostrophes?

I'm making a regex which should match everything like that : [[First example]] or [[I'm an example]].
Unfortunately, it doesn't match [[I'm an example]] because of the apostrophe.
Here it is :
preg_replace_callback('/\[\[([^?"`*%#\\\\:<>]+)\]\]/iU', ...)
Simple apostrophes (') are allowed so I really do not understand why it doesn't work.
Any ideas ?
EDIT : Here is what's happening before I'm using this regex
// This match something [[[like this]]]
$contents = preg_replace_callback('/\[\[\[(.+)\]\]\]/isU',function($matches) {
return '<blockquote>'.$matches[1].'</blockquote>';
}, $contents);
// This match something [[like that]] but doesn't work with apostrophe/quote when
// the first preg_replace_callback has done his job
$contents = preg_replace_callback('/\[\[([^?"`*%#\\\\:<>]+)\]\]/iU', ..., $contents);
try this:
$string = '[[First example]]';
$pattern = '/\[\[(.*?)\]\]/';
preg_match ( $pattern, $string, $matchs );
var_dump ( $matchs );
You can use this regex:
\[\[.*?]]
Working demo
Php code
$re = '/\[\[.*?]]/';
$str = "not match this but [[Match this example]] and not this";
preg_match_all($re, $str, $matches);
Btw, if you want to capture the content within brackets you have to use capturing groups:
\[\[(.*?)]]

extracting data between [ ] with preg_match php

i try to extract only the number beetween the [] from my response textfile:
$res1 = "MESSAGE_RESOURCE_CREATED Resource [realestate] with id [75739528] has been created.";
i use this code
$regex = '/\[(.*)\]/s';
preg_match($regex, $res1, $matches_arr);
echo $matches_arr[1];
my result is:
realestate] with id [75742084
Can someone help me ?
Use this:
$regex = '~\[\K\d+~';
if (preg_match($regex, $res1 , $m)) {
$thematch = $m[0];
// matches 75739528
}
See the match in the Regex Demo.
Explanation
\[ matches the opening bracket
The \K tells the engine to drop what was matched so far from the final match it returns
\d+ matches one or more digits
I assume you want to match what's inside the brackets, which means that you must match everything but a closing bracket:
/\[([^]]+)\]/g
DEMO HERE
Omit the g-flag in preg_match():
$regex = '/\[([^]]+)\]/';
preg_match($regex, $res1, $matches_arr);
echo $matches_arr[1]; //will output realestate
echo $matches_arr[2]; //will output 75739528
Your regex would be,
(?<=\[)\d+(?=\])
DEMO
PHP code would be,
$regex = '~(?<=\[)\d+(?=\])~';
preg_match($regex, $res1, $matches_arr);
echo $matches_arr[0];
Output:
75739528

Get the number of matched characters in a regex group

I may be pushing the boundaries of Regular Expressions, but who knows...
I'm working in php.
In something like:
preg_replace('/(?:\n|^)(={3,6})([^=]+)(\1)/','<h#>$2</h#>', $input);
Is there a way to figure out how many '=' (={3,6}) matched, so I can backreference it where the '#'s are?
Effectively turning:
===Heading 3=== into <h3>Heading 3</h3>
====Heading 4==== into <h4>Heading 4</h4>
...
You can use:
preg_replace('/(?:\n|^)(={3,6})([^=]+)(\1)/e',
"'<h'.strlen('$1').'>'.'$2'.'</h'.strlen('$1').'>'", $input);
Ideone Link
No, PCRE can't do that. You should instead use preg_replace_callback and do some character counting then:
preg_replace_callback('/(?:\n|^)(={3,6})([^=]+)(\1)/', 'cb_headline', $input);
function cb_headline($m) {
list(, $markup, $text) = $m;
$n = strlen($markup);
return "<h$n>$text</h$n>";
}
Additionally you might want to be forgiving with the trailing === signs. Don't use a backreference but allow a variable number.
You might also wish to use the /m flag for your regex, so you can keep ^ in place of the more complex (?:\n|^) assertion.
It is very simple with modifier e in regexp, no need in preg_replace_callback
$str = '===Heading 3===';
echo preg_replace('/(?:\n|^)(={3,6})([^=]+)(\1)/e',
'implode("", array("<h", strlen("$1"), ">$2</h", strlen("$1"), ">"));',
$str);
or this way
echo preg_replace('/(?:\n|^)(={3,6})([^=]+)(\1)/e',
'"<h".strlen("$1").">$2</h".strlen("$1").">"',
$str);
I would do it like this:
<?php
$input = '===Heading 3===';
$h_tag = preg_replace_callback('#(?:\n|^)(={3,6})([^=]+)(\1)#', 'paragraph_replace', $input);
var_dump($h_tag);
function paragraph_replace($matches) {
$length = strlen($matches[1]);
return "<h{$length}>". $matches[2] . "</h{$length}>";
}
?>
Output:
string(18) "<h3>Heading 3</h3>"

RegEx to handle Alphanumeric Input - PHP

I have this code.
<?php
$USClass = "3/312";
$USClass = preg_replace_callback('~[\d.]+/[\d.]+~', function ($matches) {
$parts = explode('/', $matches[0]);
return $parts[1] . ',' . $parts[0];
}, $USClass);
echo $USClass;
?>
It prints 312,3 which is what I wanted.
However, if I give an input like D12/336 then it does not work. I want it to print 336,D12
How can I do it? and What is wrong with my current code which is not handling this Alphanumeric? Is it because I used \d ?
EDIT:
I want it to handle inputs like this as well
148/DIG.111
then the output should be DIG.111,148
Yes \d does only contain digits.
You can try \w instead this is alphanumeric, but additionally it includes also _
To be Unicode you can go for
~[\pL\d.]+/[\pL\d.]+~u
\pL is a Unicode code point with the property "Letter"
The u at the end turn on the UTF-8 mode that is needed to use this feature
See http://www.php.net/manual/en/regexp.reference.unicode.php
Other solution
I think you ar e doing this a bit complicated. It would be simplier if you would make use of capturing groups.
Try this:
$in = "148/DIG.111";
preg_match_all('~([\w.]+)/([\w.]+)~', $in, $matches);
echo $matches[2][0] . ',' . $matches[1][0];
Explanation:
([\w.]+)/([\w.]+)
^^^^^^^^ ^^^^^^^^
Group 1 Group 2
Because of the brackets the matched substring is stored in the array $matches.
See here for more details on preg_match_all
With a simple preg_replace:
$USClass = "148/DIG.111";
$USClass = preg_replace('#(.+?)/(.+)#', "$2,$1", $USClass);
echo $USClass;
output:
DIG.111,148

PHP Regular expression

Some links like these:
[links url='http://www.google.com.hk' title='Google' image='']google[/links]
[links url='http://hk.yahoo.com' title='yahoo' image='']yahoo[/links]
how to use PHP Regular expression get the url attributes? Thanks.
http://www.google.com.hk
http://hk.yahoo.com
This should get you started:
preg_match_all("/\[links[^\]]+url='([^']+)'/", '{{your data}}', $arr, PREG_PATTERN_ORDER);
Explanation of the regex:
/
\[ //An excaped "[" to make it literal.
links //The work "links"
[^\]]+ //1+ non-closing brackent chars ([^] is a negative character class)
url=' //The work url='
([^']+) //The contents inside the '' in a caputuring group
/
Use this regex: /\[links\s+(?:[^\]]*\s+)*url=\'([^\']*)\'[^\]]*?\]/
$str = "[links url='http://www.google.com.hk' title='Google' image='']google[/links]";
$m = array();
preg_match('/\[links\s+(?:[^\]]*\s+)*url=\'([^\']*)\'[^\]]*?\]/', $str, $m);
echo $m[1];

Categories