[正则] 如何用正则表达式匹配? “解决马上结贴”!

zysoft17 2008-07-01 08:48:02
如何用正则表达式匹配
<sys:date />

<my:file1 />
/>前可能有空格,也可能没有空格

“解决马上结贴”!谢谢各位!
...全文
227 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
zysoft17 2008-07-01
  • 打赏
  • 举报
回复
谢谢 fxs_2008 和 wj2002160416 ,当然还有其他朋友!
zysoft17 2008-07-01
  • 打赏
  • 举报
回复
搞定,结贴总结:



$str=' <sys:date />dfsdf<my:dadfad/>dfadfjasdj <my:file1 />kjf kadflaj<img:img/img1.gif />';
preg_match_all("/<(?<keyword>[^>:]+):(?<expression>[^>:]+)\s*\/>/",$str,$tag , PREG_SET_ORDER );

wj2002160416 2008-07-01
  • 打赏
  • 举报
回复
$str='<sys:date />dfsdf <my:file1 />';
preg_match_all ("/<(\w+):(\w+)\s*\/>/",$str,$str2);
print_r($str[1]);
print_r($str[2]);
wangbo_wb 2008-07-01
  • 打赏
  • 举报
回复

$str='<sys:date />dfsdf<my:file1 />k;k;<my:file1 />werewr<my:file1/>';
echo preg_replace('/<sys:date\s*\/>|<my:file1\s*\/>/','',$str);
fxs_2008 2008-07-01
  • 打赏
  • 举报
回复
<([^>:]*?):([^>:]*?) *?/>
zysoft17 2008-07-01
  • 打赏
  • 举报
回复
兄弟们,我想要的是
<keyword> 和 <expression> 的取值!
比如:
<my: file1 />
<sys: date />
能取出
$tag[0][0] = my
$tag[0][1] = file1
$tag[1][0] = sys
$tag[1][1] = date
fxs_2008 2008-07-01
  • 打赏
  • 举报
回复
<[^>:]*?:[^>:]*?/>
剑心永远OK 2008-07-01
  • 打赏
  • 举报
回复

$str='<my:file1 />';
preg_match_all('/[^>]*>/',$str,$array);
print_r($array);

Array
(
[0] => Array
(
[0] => <my:file1 />
)

)
fxs_2008 2008-07-01
  • 打赏
  • 举报
回复
<[^>:]*?:[^>:]*?/>
试试




preg_match_all("/<[^>:]*?:[^>:]*?/>/isU", $_str, $tag, PREG_SET_ORDER);


zysoft17 2008-07-01
  • 打赏
  • 举报
回复
还有我自己写的正则是:
preg_match_all("/<(?<keyword>[_a-zA-Z0-9]+)\s*:\s*(?<expression>[^\/<>\s]+)\/>\s*/isU", $_str, $tag, PREG_SET_ORDER);

但不能识别/>前是否存在空格,如果没有空格还可以,有空格就匹配不了了。

21,886

社区成员

发帖
与我相关
我的任务
社区描述
从PHP安装配置,PHP入门,PHP基础到PHP应用
社区管理员
  • 基础编程社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧