Search code examples
regexremove-if

REGEX - Remove specified string


I need help with Regex. I have words like "testBla", "Bla" and test".
I want to cut the "test" from the "testBla". So it should only remove the "test" if the given string is larger than 4 characters. This is what i have:

^test\w{4,}

but it doesn't work.

Any ideas?


Solution

  • If you want to remove test if it occurs at the start of the line and is followed by a word character then you could use a non-word-boundary:

    ^test\B
    

    See it working online: rubular

    If you want to remove test if it occurs at the start of the line and is followed by any character (except a new line) then you could use a lookahead:

    ^test(?=.)
    

    See it working online: rubular