php - 正则表达式 - 匹配“(发布|发布|提交):”从一段文字
我需要匹配 &用正则表达式替换每个出现的字符串,如下所示:
注意: 每个字符串可以位于文本内部或文本开头;在这两种情况下,正则表达式都应该匹配从起点到行尾的所有内容
- Lorem Ipsum
发布者
某人于三月...\n 发布者:
三月某人...\n发布
三月某人...\n- Lorem Ipsum
发布者
某人...\n 作者
有人在...\n提交者:
有人在...\n- Lorem Ipsum
提交者
有人在...\n
\n
表示行尾
这就是我所做的,但接缝并不总是按预期工作。
/(?:(posted|post|submitted)\s)(?:(by))(?:(.*))\s(.*)|/i
/编辑:
好的,问题是我需要在字符串的开头和中间匹配posted|post|subscribed by
,以及by
仅在字符串的开头,否则它也会匹配诸如“by the way...”之类的内容
i need to match & replace with regex each occurence of string like theese:
note: each string can be inside a text OR at the beginning of the text; in both case the regex should match everything from the starting point to the end of the line
- Lorem Ipsum
Posted by
Someone on March...\n Posted by:
Someone on March...\nPosted
March by Someone...\n- Lorem Ipsum
Post by
Someone on...\n by
Someone on....\nSubmitted by:
Someone on...\n- Lorem Ipsum
Submitted by
Someone on...\n
\n
mean just end of line
this is what i have done, but seams to not work always as expected.
/(?:(posted|post|submitted)\s)(?:(by))(?:(.*))\s(.*)|/i
/EDIT:
ok, the problem is i need to match posted|post|submitted by
both on beginning of the string and in the middle of it, and by
only at the beginning of the string otherwise it will match also something like "by the way..."
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
更新后的
组索引 1 将是您的已发布|已提交|...以及左侧的所有内容。
浏览器中的演示
UPDATED
Group index 1 will be your Posted|Submitted|... and everything to the left.
Demo in browser
试试这个:
Try this one:
尝试:
Try:
这对我有用...
我在 regexpal (www.regexpal.com) 上对照你的列表运行了它,
祝你好运!
This works for me...
I ran that against your list on regexpal (www.regexpal.com)
Good luck!