2

I need to detect words as 'bot/hersen/levermetastase' and transform them into 'botmetastase, hersenmetastase, levermetastase'. But also 'lever/botmetastase' into 'levermetastase, botmetastase'.

So I need to be sure the "word/word/word metastase" is as variabele as possible in numbers.

This is my solution but it doesn't work.

FILTERIN:

\b(\w)\s*[\/]\s*(\w)\s*(metastase)\b 

FILTEROUT:

$1metastase, $2metastase, $3metastase
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
LaureAnne
  • 23
  • 4
  • 2
    Your question is not clear. But with regard to your regex: \w matches a single word character, not a word (this is \w+). – TonyR Sep 02 '20 at 09:01

1 Answers1

1

You may use

/?(\w+)(?=(?:/\w+)+metastase\b)/?

Replace with $1metastase (with space at the end).

If there can be spaces around the slashes, use

/?\s*(\w+)(?=(?:\s*/\s*\w+)+metastase\b)(?:\s*/)?
/?\h*(\w+)(?=(?:\h*/\h*\w+)+metastase\b)(?:\h*/)?

where \h matches a horizontal only whitespace char, and \s will match any whitespace char.

See the regex demo #1 and regex demo #2.

Details

  • /? - an optional / char
  • (\w+) - Group 1: one or more word chars
  • (?=(?:/\w+)+metastase\b) - that must be followed with
    • (?:/\w+)+ - one or more occurrences of / and then 1+ word chars
    • metastase\b - and metastase whole word (\b is a word boundary)
  • /? - an optional / char.
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
  • Thanks! If I test your answer with "hersen/bot/levermetastase" , I get "hersenmetastase, botmetastase, levermetastase, metastase" as a result but the last "metastase" doesn't belong there and is actually too much "metastase". Can you please help me again? – LaureAnne Sep 02 '20 at 09:57