2

I'm trying to replace on the following data, from Item Id: to the end of the last line that contains end:

Some junk
Item Id: 401421502995 
end: blah blah blag, Item Id: 401343012211
end: blah blah blah blah basdfasd
Some other junk

So in essence all this stuff will be replaced

Item Id: 401421502995 
end: blah blah blag, Item Id: 401343012211
end: blah blah blah blah basdfasd

Leaving me with

Some junk
Some other junk

This comes in several configurations, however the line end: is on needs to go

Here's what i have, it matches up to the end: but doesn't continue to the end of the line

(Item Id:).*?(end:)

How do i do include \n my brain is shot

TheGeneral
  • 79,002
  • 9
  • 103
  • 141
  • 2
    Try [`(?m)^Item Id:(?s:.*?)end:.*\n?` – Wiktor Stribiżew Apr 23 '18 at 07:07
  • @WiktorStribiżew that's the ticket – TheGeneral Apr 23 '18 at 07:08
  • Wait, I am not sure, maybe [`Item Id:(?s:.*?)end:(?:(?!Item Id:).)*\n?`](http://regexstorm.net/tester?p=Item+Id%3a%28%3fs%3a.*%3f%29end%3a%28%3f%3a%28%3f!Item+Id%3a%29.%29*%5cn%3f&i=Some+junk%0d%0aItem+Id%3a+401421502995+%0d%0aend%3a+blah+blah+blag%2c+Item+Id%3a+401343012211%0d%0aend%3a+blah+blah+blah+blah+basdfasd%0d%0aSome+other+junk&r=)? I see `Item Id` can start at any location, even on the `end:` line. – Wiktor Stribiżew Apr 23 '18 at 07:11
  • @WiktorStribiżew yep that last one seems to work, answer it up and get the gravey – TheGeneral Apr 23 '18 at 07:15

1 Answers1

2

You may use

Item Id:(?s:.*?)end:(?:(?!Item Id:).)*\n?

See the regex demo.

Details

  • Item Id: - a literal substring
  • (?s:.*?) - a modifier group with the RegexOptions.Singleline option enabled
  • end: - a literal substring
  • (?:(?!Item Id:).)* - 0+ occurrences of any char but LF that does not start an Item Id: char sequence (see more details on this construct here)
  • \n? - an optional LF char (it is optional to also match the last line in the input).
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
  • 1
    Awesome and thanks, its like 5:20 pm here, and my brain is already at home – TheGeneral Apr 23 '18 at 07:20
  • It is 9:21 AM here :) and my brain is slowly turning on :) – Wiktor Stribiżew Apr 23 '18 at 07:21
  • BTW, a synonymous expression is [`Item Id:(?s:.*?)end:.*?(?:(?=Item Id:)|\r\n?|$)`](http://regexstorm.net/tester?p=Item+Id%3a%28%3fs%3a.*%3f%29end%3a.*%3f%28%3f%3a%28%3f%3dItem+Id%3a%29%7c%5cr%5cn%3f%7c%24%29&i=Some+junk%0d%0aItem+Id%3a+401421502995+%0d%0aend%3a+blah+blah+blag%2c+Item+Id%3a+401343012211%0d%0aend%3a+blah+blah+blah+blah+basdfasd%0d%0aSome+other+junk&r=). – Wiktor Stribiżew Apr 23 '18 at 07:23