0

I need to match only the start of string like these with mainly pattern in "KEYWORD AAA/0010/0 " with a space after /0 and replace it with any other string. It has to happen for only the instance in the start.

KEYWORD AAA/0010/0 
KEYWORD AAAAAA/0010/0
KEYWORD AAA/001000/0
KEYWORD AAA/0010/000
KEYWORD AAA/0010/0 Testing comment
KEYWORD AAA/0010/0 Testing comment KEYWORD AAA/0010/0 Testing comment
KEYWORD */*/*
KEYWORD ?/?/?

I have tried this but it does not distinguish between the instances at start and middle and it does not match the last one

^KEYWORD .*\/.*\/.*\s

Any help would be highly appreciated.

AD.Net
  • 13,352
  • 2
  • 28
  • 47
  • It is not clear which exact part of the expression you are trying to match in order to replace. – Matanya Dec 12 '12 at 15:24

3 Answers3

1

Ungreedy .*?'s (match until next /, or even [^/]*), and optional \s with \s? (for the last line), making ^KEYWORD .*?\/.*?\/.*?\s? or ^KEYWORD [^\/]*\/[^\/]*\/[^\/]*\s?

Wrikken
  • 69,272
  • 8
  • 97
  • 136
1

I think you're almost there. Your regex isn't matching your last test case because there isn't a space at the end. Try this instead:

/^KEYWORD .*\/.*\/.*(\s|$)/
FixMaker
  • 3,759
  • 3
  • 26
  • 44
  • This one almost works but it matches all instances, not only the first instance in a line. May be my question was not clear. +1 – AD.Net Dec 12 '12 at 16:20
1

What about

^KEYWORD [^\s/]*\/[^\s/]*\/[^\s/]*(?:\s|$)

See it here on Regexr

The last row is not matched, because there is no space following, therefore I replaced it by Whitespace or the end of the string (?:\s|$).

stema
  • 90,351
  • 20
  • 107
  • 135