php - 正则表达式 - 匹配“(发布|发布|提交):”从一段文字

发布于 2024-10-28 00:18:27 字数 781 浏览 2 评论 0原文

我需要匹配 &用正则表达式替换每个出现的字符串,如下所示:

注意: 每个字符串可以位于文本内部或文本开头;在这两种情况下,正则表达式都应该匹配从起点到行尾的所有内容

  • 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...\n
  • Posted March by Someone...\n
  • Lorem Ipsum Post by Someone on...\n
  • by Someone on....\n
  • Submitted 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

故事未完 2024-11-04 00:18:27

更新后的

/((?:^by\s)|(?:.*(?:Post|Submit[t]?)(?:ed)?[\w|\s]*by:?\s?))/im

组索引 1 将是您的已发布|已提交|...以及左侧的所有内容。

浏览器中的演示

“在此输入图像描述”

UPDATED

/((?:^by\s)|(?:.*(?:Post|Submit[t]?)(?:ed)?[\w|\s]*by:?\s?))/im

Group index 1 will be your Posted|Submitted|... and everything to the left.

Demo in browser

enter image description here

追星践月 2024-11-04 00:18:27

试试这个:

preg_replace ('/((^by[^a-z])|(.*?(posted|post|submitted))).*\n?/im', '', $text);

Try this one:

preg_replace ('/((^by[^a-z])|(.*?(posted|post|submitted))).*\n?/im', '', $text);
倾其所爱 2024-11-04 00:18:27

尝试:

/((post(ed)?|submitted)\s)?(by|[a-z]* by):? .*/i

Try:

/((post(ed)?|submitted)\s)?(by|[a-z]* by):? .*/i
洛阳烟雨空心柳 2024-11-04 00:18:27

这对我有用...

((Post|Posted|Submitted).*)|(by [\w]+ on).*

我在 regexpal (www.regexpal.com) 上对照你的列表运行了它,

祝你好运!

This works for me...

((Post|Posted|Submitted).*)|(by [\w]+ on).*

I ran that against your list on regexpal (www.regexpal.com)

Good luck!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文