help me to improve my existing regex code
I'm using this
/\{\$?([^# ]+)\#?([^ ]+)? ?([^|]+)?[ \|]?([^\}]+)?\}/
perl-compatible regular expressionFor matching strings like :
{$string#asdf#asdf can you hear me? |ucfirst|strtoupper}
Outputting this :
Array
(
[0] => {$string#asdf#asdf can you hear me? |ucfirst|strtoupper}
[1] => string
[2] => asdf#asdf
[3] => can you hear me?
[4] => ucfirst|strtoupper
)
if used with PHP's preg_replace_callback
function,
but with string like {$string#asdf#asdf can you hear me? ucfirst|strtoupper}
it's outputting this :
Array
(
[0] => {$string#asdf#asdf can you hear me? ucfirst|strtoupper}
[1] => string
[2] => asdf#asdf
[3] => can you hear me? ucfirst
[4] => strtoupper
)
can you improve it the way you wish so that it will be able to match string like :
{$string#asdf#asdf can you hear me? ucfirst|strtoupper}
(notice: "|" removed in front of ucfirst) without affecting 开发者_JAVA百科the result of the array.(ie output should be the same as the first printed array above)This Regex string already matches both of the samples you provided. What are you really asking here?
Not clear exactly what you're wanting, but here's my best guess based on what you specified:
{\$([^#]*)#(\S*) (.*?) (\S*)}
That will give the following for matches 1-4
- Everything up to the first # sign
- Everything after the first # sign and before the next space
- Everything else between #2 and #4
- The last string of characters not containing a space
In the example
{$string#asdf#asdf can you hear me? ucfirst|strtoupper}
We have:
- string
- asdf#asdf
- can you hear me?
- ucfirst|strtoupper
精彩评论