开发者

help me to improve my existing regex code

I'm using this

/\{\$?([^# ]+)\#?([^ ]+)? ?([^|]+)?[ \|]?([^\}]+)?\}/

perl-compatible regular expression

For 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?

help me to improve my existing regex code


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

  1. Everything up to the first # sign
  2. Everything after the first # sign and before the next space
  3. Everything else between #2 and #4
  4. The last string of characters not containing a space

In the example

{$string#asdf#asdf can you hear me? ucfirst|strtoupper}

We have:

  1. string
  2. asdf#asdf
  3. can you hear me?
  4. ucfirst|strtoupper
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜