开发者

php - regex - match "(posted|post|submitted) by:" from a text

开发者 https://www.devze.com 2023-02-19 21:38 出处:网络
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 f

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 Som开发者_JS百科eone 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..."


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

php - regex - match "(posted|post|submitted) by:" from a text


Try this one:

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


Try:

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


This works for me...

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

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

Good luck!

0

精彩评论

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