Reputation: 10514
I am looking for a regex that collects everything between the last colon (:) and the first space before it. So if I have the sentence "HUHweg: 4ihfw:euigr yalalala:"
I want to collect "yalalala"
.
I came up with " (?<content>)/[^:]*$"
but the content group is empty. I expect that a space followed by the content untill the last
:
from the end ($
) would work, but it does not. How can it be made to work?
Upvotes: 2
Views: 325
Reputation: 1046
PHP code (I think that it can be easily converted into other language)
<?php
$patterns=array('/(.|\s)*(\s)((.[^:])*)(\:)(?=[^\:]*)/');
$replacements=array(' $3 ');
$string='HUHweg: 4ihfw:euigr yalalala: ';
echo preg_replace($patterns,$replacements,$string);
?>
Result: yalalala
First comes any amount of any char or white char. Then comes exactly one white space. Then our string, which is captured as $3
. Next thing is colon, and then statement which matches any char but not colon :)
Upvotes: 0
Reputation: 9644
You're not so far... Try using (here I use ~
as regex delimiters)
~ (?<content>[^ :]*):\s*$~
The capturing group has to have, well, something to capture. Also you need to forbid spaces in the content, or you might catch some words before the part you want.
The \s
, a shortcut for all kind of whitespaces (spaces, tabs, newline...) will catch the case where there are trailing whitespaces after the last column.
See demo
Upvotes: 3