开发者

Regex to get string after a character ending with another optional character and not matching it

I'm struggling with a regex, here's an exampl开发者_StackOverflow社区e of the input string

test#string=val

i need to get "string" no matter what's inside it (including special charso but bviously not including the '=') it's always preceded by the '#' character, but i don't need it included. The word ahead of it ('test' in this case), is also optional.

The '=' is optional, but if it's there i need it to be ignored. I've been using something like this

#([\w\W]*)

But i can't manage to handle the optional '='

Thanks in advance for any help.


Try this regular expression:

#([^=]*)

This matches anything after # that is not a =. It doesn’t matter whether there is actually a = following or not.


You didn't specify which language. In php you can do:

<?php
preg_match('~#(([^=#]*))~', $str, $m );
var_dump($m[1]); // $m[1] is your string here
?>


In this case I think it's better if you use simpler functions like explode(,'#') and/or strpos.

They are suggested for simple string manipulation like this

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜