Matching two words with some characters in between in regular expression

前端 未结 4 463
孤独总比滥情好
孤独总比滥情好 2021-01-07 14:55

I want to do a match for a string when no abc is followed by some characters (possibly none) and ends with .com.

I tried with the following

4条回答
  •  心在旅途
    2021-01-07 15:54

    It's unclear from your wording if you want to match a string ending with .com AND NOT containing abc before that; or to match a string that doesn't have "abc followed by characters followed by .com".

    Meaning, in the first case, "def.edu" does NOT match (no "abc" but doesn't end with ".com") but in the second case "def.edu" matches (because it's not "abcSOMETHING.com")


    In the first case, you need to use negative look-behind:

    (?

    IMPORTANT: your original expression using look-behind - #3 ( (? ) - didn't work because look-behind ONLY looks behind immediately preceding the next term. Therefore, the "something after abc" should be included in the look-behind together with abc - as my RegEx above does.

    PROBLEM: my RegEx above likely won't work with your specific RegEx Engine, unless it supports general look-behinds with variable length expression (like the one above) - which ONLY .NET does these days (A good summary of what does and doesn't support what flavors of look-behind is at http://www.regular-expressions.info/lookaround.html ).

    If that is indeed the case, you will have to do double match: first, check for .com; capturing everything before it; then negative match on abc. I will use Perl syntax since you didn't specify a language:

    if (/^(.*)\.com$/) {
        if ($1 !~ /abc/) { 
        # Or, you can just use a substring:
        # if (index($1, "abc") < 0) {
            # PROFIT!
        }
    }
    

    In the second case, the EASIEST thing to do is to do a "does not match" operator - e.g. !~ in Perl (or negate a result of a match if your language doesn't support "does not match"). Example using pseudo-code:

    if (NOT string.match(/abc.+\.com$/)) ...
    

    Please note that you don't need ".+"/".*" when using negative lookbehind;

提交回复
热议问题