-2

I am struggling with a REGEX and I can't find a solution. I have the following strings:

LSUS_GDIS_SHIP_0211e947-1587-11de-9967-99f27557554e.txt
LSUS_GDIS_REC_0211e947-1587-11de-9967-99f27557554e.txt
LSUS_GDIS_CAPACITY_0211e947-1587-11de-9967-99f27557554e.txt

I need to match each line except SHIP or REC or CAPACITY.

I came up with

(?!.*\bSHIP\b|\bREC\b|\bCAPACITY\b).*

How can I exclude SHIP or REC or CAPACITY? When I do

(SHIP|REC|CAPACITY)

I only match these strings but I want to exclude them.

Nimantha
  • 6,405
  • 6
  • 28
  • 69
Peter
  • 1,786
  • 4
  • 21
  • 40
  • You may *remove* them with your pattern. Please clarify your scenario. – Wiktor Stribiżew Dec 20 '18 at 13:48
  • 3
    Also, please tell us which language or regex tool you are using. There are more than one way to approach this. – Tim Biegeleisen Dec 20 '18 at 13:49
  • Hello both, it is a java system but I can't code it myself. I only have the possibility of setting a regex expression and then a "replacement" value. I need to replace everything but SHIP, REC or CAPACITY. – Peter Dec 20 '18 at 13:50
  • If the "duplicate" link I provided does not solve your problem, please add more details to your question. – PM 77-1 Dec 20 '18 at 13:51
  • Replace with what? please show expected outputs on those three lines. – revo Dec 20 '18 at 13:57
  • Try `(SHIP|REC|CAPACITY)?.` to replace with `$1` – Wiktor Stribiżew Dec 20 '18 at 14:05
  • I would split it into two parts: Replace the string before the mentioned words and then replace after the words. Regex for before; `(.+?)(?=((SHIP)|(REC)|(CAPACITY)))` and after `(?<=(SHIP)|(REC)|(CAPACITY)).+` whereas the positive look-behind (`?<=`) may not be supported. – LittleBit Dec 20 '18 at 14:14

2 Answers2

2

Each line can be broken into three parts with the regex

([A-Z_]+)(SHIP|REC|CAPACITY)([_a-z\d-\.]+)

Then you can use replacement as

replacement1+$2+replacement2
Nimantha
  • 6,405
  • 6
  • 28
  • 69
Brij Raj Kishore
  • 1,595
  • 1
  • 11
  • 24
1

Use regular expression to match group wise:

^([\s\S]*?)(SHIP|REC|CAPACITY)([\s\S]*?)$.*

Description: For result check this link

  • ^ « Matches the start of a string without consuming any characters. If multiline mode is used, this will also match immediately after a newline character.
  • 1st,3rd Capturing Groups ([\s\S]*?)
    • \s « Matches any space, tab or newline character.
    • \S « Matches anything other than a space, tab or newline.
    • [\s\S] « match any character
    • *? Quantifier — Matches between zero and unlimited times, as few times as possible, expanding as needed (lazy)
  • 2nd Capturing Group (SHIP|REC|CAPACITY), matches these words as 2nd group
  • $ asserts position at the end of a line
    • .* matches any character (except for line terminators)
Yash
  • 9,250
  • 2
  • 69
  • 74