1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2012
    Rep Power

    Regex doesnt work on img tags in Html

    Hey Guys,

    I'm an absolute beginner with regular expressions, but need some help with my expression.

    I'm having a java string:

    "TAG'G' CLTG TAG hello CLTG"

    and my expression to remove everything between the tags "TAG" and "CLTG":


    If I test the regex on my string, and replace it with nothing (string.replaceAll(regex, "") , It works only with the second Tag, but not with the first TAG.

    This is no logical connection for me, why the first one works and the second one doesnt .
    Maybe you can help me?

    greetz arma
  2. #2
  3. Turn left at the third duck
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2011
    Nelson, NZ
    Rep Power
    Hi armaggadon,

    Welcome to the DevShed regex board.
    You bring a fantastic beginner question.
    Here are some elements to help you understand what is going wrong.

    1. [^CLTG] does not mean "not CLTG", but "any ONE character that is not a C, a L, a T or a G".

    2. You probably have case-insensitive mode on? If so, on the second tag (TAG hello CLTG), the regex will fail on the first "l" in hello, as it is trying to match any one character that is not a L (or a c, t, or g).

    3. Here is a way to match your entire tag:
    It matches TAG, then
    any character that is NOT a C, OR a C that is not followed by LTG,
    zero or more times,
    then CLTG.

    The (?! is a negative lookahead from regex lookaround syntax.

    You probably know that by adding parentheses, you could "capture" the text between TAG and CLTG, if for whatever reason you needed it later in your code or replacement.

    Hope this helps, let me know if you have any questions!
    Wishing you a fun weekend.

IMN logo majestic logo threadwatch logo seochat tools logo