Regex match from right to left
up vote
0
down vote
favorite
I wanted to match something from right to left, below is one of such example.
100abababab3x3x3xx1000morewords
If i want to match something between and last xx
and immediate previous ab
and get 3x3x3
I tried something like below , but it matches ababab3x3x3
preg_match('/ab(.*?)xx/',$text,$totmat);
Note : please don't recommend strrev.
Above example is just for illustration , all i wanted to do is match from right to left.
php regex
|
show 5 more comments
up vote
0
down vote
favorite
I wanted to match something from right to left, below is one of such example.
100abababab3x3x3xx1000morewords
If i want to match something between and last xx
and immediate previous ab
and get 3x3x3
I tried something like below , but it matches ababab3x3x3
preg_match('/ab(.*?)xx/',$text,$totmat);
Note : please don't recommend strrev.
Above example is just for illustration , all i wanted to do is match from right to left.
php regex
1
Cthulhu is coming! Joke apart, it is not actually text but a possibly nestedHTML
structure - use a parser and appropriatexpath
queries instead.
– Jan
Nov 9 at 15:51
there are like 20 different tables and that page does not follow any w3 standards , so please answer it in general regex way. how to match 333 from ababab333xx1000morewords perspective
– Gracie williams
Nov 9 at 15:55
Nope. See, the point is,xpath
queries in combination with a regular expression are really powerful and you should really use them. Regular expressions alone lead to a dead end here, really.
– Jan
Nov 9 at 15:58
Yes i understand , please see my edit.
– Gracie williams
Nov 9 at 15:59
Regex is not the best for what you are trying to achieve - look into php's DOMDocument - php.net/manual/en/class.domdocument.php
– Stuart
Nov 9 at 16:07
|
show 5 more comments
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I wanted to match something from right to left, below is one of such example.
100abababab3x3x3xx1000morewords
If i want to match something between and last xx
and immediate previous ab
and get 3x3x3
I tried something like below , but it matches ababab3x3x3
preg_match('/ab(.*?)xx/',$text,$totmat);
Note : please don't recommend strrev.
Above example is just for illustration , all i wanted to do is match from right to left.
php regex
I wanted to match something from right to left, below is one of such example.
100abababab3x3x3xx1000morewords
If i want to match something between and last xx
and immediate previous ab
and get 3x3x3
I tried something like below , but it matches ababab3x3x3
preg_match('/ab(.*?)xx/',$text,$totmat);
Note : please don't recommend strrev.
Above example is just for illustration , all i wanted to do is match from right to left.
php regex
php regex
edited Nov 9 at 16:31
asked Nov 9 at 15:44
Gracie williams
558
558
1
Cthulhu is coming! Joke apart, it is not actually text but a possibly nestedHTML
structure - use a parser and appropriatexpath
queries instead.
– Jan
Nov 9 at 15:51
there are like 20 different tables and that page does not follow any w3 standards , so please answer it in general regex way. how to match 333 from ababab333xx1000morewords perspective
– Gracie williams
Nov 9 at 15:55
Nope. See, the point is,xpath
queries in combination with a regular expression are really powerful and you should really use them. Regular expressions alone lead to a dead end here, really.
– Jan
Nov 9 at 15:58
Yes i understand , please see my edit.
– Gracie williams
Nov 9 at 15:59
Regex is not the best for what you are trying to achieve - look into php's DOMDocument - php.net/manual/en/class.domdocument.php
– Stuart
Nov 9 at 16:07
|
show 5 more comments
1
Cthulhu is coming! Joke apart, it is not actually text but a possibly nestedHTML
structure - use a parser and appropriatexpath
queries instead.
– Jan
Nov 9 at 15:51
there are like 20 different tables and that page does not follow any w3 standards , so please answer it in general regex way. how to match 333 from ababab333xx1000morewords perspective
– Gracie williams
Nov 9 at 15:55
Nope. See, the point is,xpath
queries in combination with a regular expression are really powerful and you should really use them. Regular expressions alone lead to a dead end here, really.
– Jan
Nov 9 at 15:58
Yes i understand , please see my edit.
– Gracie williams
Nov 9 at 15:59
Regex is not the best for what you are trying to achieve - look into php's DOMDocument - php.net/manual/en/class.domdocument.php
– Stuart
Nov 9 at 16:07
1
1
Cthulhu is coming! Joke apart, it is not actually text but a possibly nested
HTML
structure - use a parser and appropriate xpath
queries instead.– Jan
Nov 9 at 15:51
Cthulhu is coming! Joke apart, it is not actually text but a possibly nested
HTML
structure - use a parser and appropriate xpath
queries instead.– Jan
Nov 9 at 15:51
there are like 20 different tables and that page does not follow any w3 standards , so please answer it in general regex way. how to match 333 from ababab333xx1000morewords perspective
– Gracie williams
Nov 9 at 15:55
there are like 20 different tables and that page does not follow any w3 standards , so please answer it in general regex way. how to match 333 from ababab333xx1000morewords perspective
– Gracie williams
Nov 9 at 15:55
Nope. See, the point is,
xpath
queries in combination with a regular expression are really powerful and you should really use them. Regular expressions alone lead to a dead end here, really.– Jan
Nov 9 at 15:58
Nope. See, the point is,
xpath
queries in combination with a regular expression are really powerful and you should really use them. Regular expressions alone lead to a dead end here, really.– Jan
Nov 9 at 15:58
Yes i understand , please see my edit.
– Gracie williams
Nov 9 at 15:59
Yes i understand , please see my edit.
– Gracie williams
Nov 9 at 15:59
Regex is not the best for what you are trying to achieve - look into php's DOMDocument - php.net/manual/en/class.domdocument.php
– Stuart
Nov 9 at 16:07
Regex is not the best for what you are trying to achieve - look into php's DOMDocument - php.net/manual/en/class.domdocument.php
– Stuart
Nov 9 at 16:07
|
show 5 more comments
3 Answers
3
active
oldest
votes
up vote
1
down vote
Not sure this is the most optimized way or not? But this will work for you if you use the combination of Look ahead positive (?=)
and Look behind positive (?<=)
. See regex
<?php
$re = '/w+(?<=ab)(.*?)(?=xx)/m';
$str = '100abababab3x3x3xx1000morewords';
preg_match_all($re, $str, $matches, PREG_SET_ORDER, 0);
// Print the full matched result
echo $matches[0][1];
DEMO: https://3v4l.org/db69N
add a comment |
up vote
0
down vote
$str = '100abababab3x3x3xx1000morewords';
preg_match('/ab((?:(?!ab).)*)xx/', $str, $m);
print_r($m);
Output:
Array
(
[0] => ab3x3x3xx
[1] => 3x3x3
)
>Explanation:
ab : literally ab
( : start group 1
(?: : start non capture group
(?!ab) : negative lookahead, make sure we doon't have ab
. : any character but newline
)* : end group, may appear 0 or more times
) : end group 1
xx : literally xx
add a comment |
up vote
0
down vote
There are other approaches than a regex to this kind of problem that would be close to twice faster in computing time.
Here for example :
$str = "100abababab3x3x3xx1000morewords";
$result = explode("ab", explode("xx", $str)[0]);
var_dump(end($result));
First occurence of explode split the string in two between the "xx" characters. We're only interested by the left part (Index 0).
Second occurence of explode split the string with the caracters ab. We're only interested by the last occurence of ab. Therefore var_dump(end($result));
prints the expected result.
add a comment |
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
Not sure this is the most optimized way or not? But this will work for you if you use the combination of Look ahead positive (?=)
and Look behind positive (?<=)
. See regex
<?php
$re = '/w+(?<=ab)(.*?)(?=xx)/m';
$str = '100abababab3x3x3xx1000morewords';
preg_match_all($re, $str, $matches, PREG_SET_ORDER, 0);
// Print the full matched result
echo $matches[0][1];
DEMO: https://3v4l.org/db69N
add a comment |
up vote
1
down vote
Not sure this is the most optimized way or not? But this will work for you if you use the combination of Look ahead positive (?=)
and Look behind positive (?<=)
. See regex
<?php
$re = '/w+(?<=ab)(.*?)(?=xx)/m';
$str = '100abababab3x3x3xx1000morewords';
preg_match_all($re, $str, $matches, PREG_SET_ORDER, 0);
// Print the full matched result
echo $matches[0][1];
DEMO: https://3v4l.org/db69N
add a comment |
up vote
1
down vote
up vote
1
down vote
Not sure this is the most optimized way or not? But this will work for you if you use the combination of Look ahead positive (?=)
and Look behind positive (?<=)
. See regex
<?php
$re = '/w+(?<=ab)(.*?)(?=xx)/m';
$str = '100abababab3x3x3xx1000morewords';
preg_match_all($re, $str, $matches, PREG_SET_ORDER, 0);
// Print the full matched result
echo $matches[0][1];
DEMO: https://3v4l.org/db69N
Not sure this is the most optimized way or not? But this will work for you if you use the combination of Look ahead positive (?=)
and Look behind positive (?<=)
. See regex
<?php
$re = '/w+(?<=ab)(.*?)(?=xx)/m';
$str = '100abababab3x3x3xx1000morewords';
preg_match_all($re, $str, $matches, PREG_SET_ORDER, 0);
// Print the full matched result
echo $matches[0][1];
DEMO: https://3v4l.org/db69N
edited Nov 10 at 8:05
answered Nov 9 at 16:38
Curious_MInd
13.2k32340
13.2k32340
add a comment |
add a comment |
up vote
0
down vote
$str = '100abababab3x3x3xx1000morewords';
preg_match('/ab((?:(?!ab).)*)xx/', $str, $m);
print_r($m);
Output:
Array
(
[0] => ab3x3x3xx
[1] => 3x3x3
)
>Explanation:
ab : literally ab
( : start group 1
(?: : start non capture group
(?!ab) : negative lookahead, make sure we doon't have ab
. : any character but newline
)* : end group, may appear 0 or more times
) : end group 1
xx : literally xx
add a comment |
up vote
0
down vote
$str = '100abababab3x3x3xx1000morewords';
preg_match('/ab((?:(?!ab).)*)xx/', $str, $m);
print_r($m);
Output:
Array
(
[0] => ab3x3x3xx
[1] => 3x3x3
)
>Explanation:
ab : literally ab
( : start group 1
(?: : start non capture group
(?!ab) : negative lookahead, make sure we doon't have ab
. : any character but newline
)* : end group, may appear 0 or more times
) : end group 1
xx : literally xx
add a comment |
up vote
0
down vote
up vote
0
down vote
$str = '100abababab3x3x3xx1000morewords';
preg_match('/ab((?:(?!ab).)*)xx/', $str, $m);
print_r($m);
Output:
Array
(
[0] => ab3x3x3xx
[1] => 3x3x3
)
>Explanation:
ab : literally ab
( : start group 1
(?: : start non capture group
(?!ab) : negative lookahead, make sure we doon't have ab
. : any character but newline
)* : end group, may appear 0 or more times
) : end group 1
xx : literally xx
$str = '100abababab3x3x3xx1000morewords';
preg_match('/ab((?:(?!ab).)*)xx/', $str, $m);
print_r($m);
Output:
Array
(
[0] => ab3x3x3xx
[1] => 3x3x3
)
>Explanation:
ab : literally ab
( : start group 1
(?: : start non capture group
(?!ab) : negative lookahead, make sure we doon't have ab
. : any character but newline
)* : end group, may appear 0 or more times
) : end group 1
xx : literally xx
answered Nov 9 at 16:52
Toto
63.8k175697
63.8k175697
add a comment |
add a comment |
up vote
0
down vote
There are other approaches than a regex to this kind of problem that would be close to twice faster in computing time.
Here for example :
$str = "100abababab3x3x3xx1000morewords";
$result = explode("ab", explode("xx", $str)[0]);
var_dump(end($result));
First occurence of explode split the string in two between the "xx" characters. We're only interested by the left part (Index 0).
Second occurence of explode split the string with the caracters ab. We're only interested by the last occurence of ab. Therefore var_dump(end($result));
prints the expected result.
add a comment |
up vote
0
down vote
There are other approaches than a regex to this kind of problem that would be close to twice faster in computing time.
Here for example :
$str = "100abababab3x3x3xx1000morewords";
$result = explode("ab", explode("xx", $str)[0]);
var_dump(end($result));
First occurence of explode split the string in two between the "xx" characters. We're only interested by the left part (Index 0).
Second occurence of explode split the string with the caracters ab. We're only interested by the last occurence of ab. Therefore var_dump(end($result));
prints the expected result.
add a comment |
up vote
0
down vote
up vote
0
down vote
There are other approaches than a regex to this kind of problem that would be close to twice faster in computing time.
Here for example :
$str = "100abababab3x3x3xx1000morewords";
$result = explode("ab", explode("xx", $str)[0]);
var_dump(end($result));
First occurence of explode split the string in two between the "xx" characters. We're only interested by the left part (Index 0).
Second occurence of explode split the string with the caracters ab. We're only interested by the last occurence of ab. Therefore var_dump(end($result));
prints the expected result.
There are other approaches than a regex to this kind of problem that would be close to twice faster in computing time.
Here for example :
$str = "100abababab3x3x3xx1000morewords";
$result = explode("ab", explode("xx", $str)[0]);
var_dump(end($result));
First occurence of explode split the string in two between the "xx" characters. We're only interested by the left part (Index 0).
Second occurence of explode split the string with the caracters ab. We're only interested by the last occurence of ab. Therefore var_dump(end($result));
prints the expected result.
answered Nov 9 at 17:07
Lou
676213
676213
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53228926%2fregex-match-from-right-to-left%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
Cthulhu is coming! Joke apart, it is not actually text but a possibly nested
HTML
structure - use a parser and appropriatexpath
queries instead.– Jan
Nov 9 at 15:51
there are like 20 different tables and that page does not follow any w3 standards , so please answer it in general regex way. how to match 333 from ababab333xx1000morewords perspective
– Gracie williams
Nov 9 at 15:55
Nope. See, the point is,
xpath
queries in combination with a regular expression are really powerful and you should really use them. Regular expressions alone lead to a dead end here, really.– Jan
Nov 9 at 15:58
Yes i understand , please see my edit.
– Gracie williams
Nov 9 at 15:59
Regex is not the best for what you are trying to achieve - look into php's DOMDocument - php.net/manual/en/class.domdocument.php
– Stuart
Nov 9 at 16:07