开发者

regex to match all *texttexttext in a string

开发者 https://www.devze.com 2023-03-13 10:15 出处:网络
I need a regex that will match all strings starting with * until it meets a < so in this 开发者_StackOverflowblock of text: bob went to the *store and he bought a toy and <then he went outside

I need a regex that will match all strings starting with * until it meets a <

so in this 开发者_StackOverflowblock of text: bob went to the *store and he bought a toy and <then he went outside *and went to his car for <a ride.

it would match bob went to the *store and he bought a toy and and and went to his car for

and if there is no "<" it will match all until the end of the line


try this:

<pre>
<?
$s = 'bob went to the *store and he bought a toy and <then he went outside *and went to his car for <a ride.';

preg_match_all("/\*([^<]+)/", $s, $matched); 
print_r($matched);
?>

output:

Array
(
    [0] => Array
        (
            [0] => *store and he bought a toy and 
            [1] => *and went to his car for 
        )

    [1] => Array
        (
            [0] => store and he bought a toy and 
            [1] => and went to his car for 
        )

)


It should be something like:

With PHP:

preg_match_all("#\*(.+?)<#", $stringWithText, $matches, PREG_SET_ORDER);
$mCount = count($matches);

foreach ($matches as $match)
    echo "Matched: " . $match[1] . "<br/>";

If you want to skip the end "<" the change the expression to #\*(.+?)<?# and if you want to allow line changes, use these flags:

preg_match_all("#\*(.+?)<#si", $stringWithText, $matches, PREG_SET_ORDER);

Notice the si flags trailing the expression

Hope it helps


Try \*(.+?)<

You can use this tool to experiment with regex: http://gskinner.com/RegExr/

EDIT To keep matching to a < OR to the end of the string, use:

\*(.+?)[<|$.*]


I would use something like this

(?<=\*).*?(?=<|$)

See it here on Regexr

(?<=\*) is a look behind, it matches no character, but it ensures, that the character before is a *

.*? matches everything non greedy

(?=<|$) is a look ahead, it matches no character, but it ensures, that the following character is a < or a $ (row end) ==> use the m (multiline) modifier, otherwise the $ will match only the end of the string and not the end of the row.

0

精彩评论

暂无评论...
验证码 换一张
取 消