I wanna extract these two words from the following string
Code: Select all
String anotherstringI've tried this regex
Code: Select all
(.*?) ([^\1]+?)e.g. from hello hello extract only the first hello but from hello hi extract both hello and hi.
Moderator: General Moderators
Code: Select all
String anotherstringCode: Select all
(.*?) ([^\1]+?)Code: Select all
(.*?) ([\1]+?)Code: Select all
(.*?) ([^\1]+?)Code: Select all
<?php
function conditionalExplode($input)
{
$explo = explode(' ', $input);
if ($explo[0] != $explo[1]) {
unset($explo[1]);
}
return $explo;
}
function conditionalMatch($input)
{
$matches = array();
preg_match('~(\w+)\s+(\1)?~', $input, $matches);
unset($matches[0]);
return array_values($matches);
}
assert_options(ASSERT_ACTIVE, true);
assert_options(ASSERT_WARNING, true);
assert(($out = conditionalMatch('string string')) == array('string', 'string')); var_dump($out);
assert(($out = conditionalMatch('hello hello')) == array('hello', 'hello')); var_dump($out);
assert(($out = conditionalMatch('hello everybody')) == array('hello')); var_dump($out);
assert(($out = conditionalMatch('string differentstring')) == array('string')); var_dump($out);Code: Select all
(\w+)\s+([^\1])?I've tried this regex but its not at all workingI wrote:It will extract both 'test' and 'text' from "test text"
and only the first 'test' from "test test".
Code: Select all
(\w+)\s+([^\1])?yes it is very simple logic which is way then you use the logic functionality of PHP it is very easy to do. Regular expressions are not really a logical language. You can use a couple of prebaked conditions but that's all; mostly the kind of conditions that are difficult to perform using conventional logic. So combined with conventional logic regular expression are very powerful indeed. On their own, they are not.I belief there must be a way to do it with regex (AS THE LOGIC IS VERY SIMPLE). and I wanna know that.
I wrote:So why does this have to be regex only? and why do you need to match in such a precise and bizarre way?
I want to learn how to do it with regex. and also if I can do it only with regex It will save lots of lines of my codes as onli 1 line of preg_match will work.You wrote:Could you answer my questions now. Seeing as I have spent quite a lot of time on this I feel I am entitled to an answer. I think you will benefit from my response as well.I wrote:So why does this have to be regex only? and why do you need to match in such a precise and bizarre way?
I have tried usingYou wrote:[^\1] is never going to work because [] is for matching sets and not whole strings. [abc] matches 'a' or 'b 'or 'c' not 'abc'. So unless someone can figure out some really hacky way of doing it I can't see how it is possible.
But it doesn't works.I wrote:(\w+)\s+((?:^\1))?
No offence but that's a really crap reason to try and do something that is really difficult. What exactly is wrong with lines of code?I want to learn how to do it with regex. and also if I can do it only with regex It will save lots of lines of my codes as onli 1 line of preg_match will work.
No, there isn't.are there anything that mean NOT in () ??