ealeon
ealeon

Reputation: 12452

Perl regex: look for keyword which are not starting with

Example 1: "hello this is me. KEYWORD: blah"
Example 2: "KEYWORD: apple"

I just want to be able to catch KEYWORD in example 1, not 2 since in 2, it starts with KEYWORD

if ($line =~/KEYWORD:/x) {
    # do something
}

The above code catch both examples. How can I change regex so that it only catches KEYWORD in example 1?

PS Eventually I want example 1 to be KEYWORD: blah

Upvotes: 0

Views: 91

Answers (5)

ikegami
ikegami

Reputation: 385917

The answer is actually quite simple!

/.KEYWORD/    # Not at the start of a line
/.KEYWORD/s   # Not at the start of the string

By the way, you might want to add \b before KEYWORD to avoid matching NOTTHEKEYWORD.

Upvotes: 3

PerlDuck
PerlDuck

Reputation: 5730

You are looking for a negative lookbehind assertion, i.e. for 'KEYWORD' that is not preceeded by a certain string (in your case the start-of-line marker ^):

if ($line =~/(?<!^)KEYWORD:/x) {
    # found KEYWORD in '$line', but not at the beginning
    print $line, "\n";
}

Output:

hello this is me. KEYWORD: blah

Update: As stated in the comments, the /x modifier isn't necessary in my first regex but can be used to make the pattern more readable. It allows for whitespace (including newlines) and/or comments in the pattern to improve readability. The downside is that every blank/space character in the actual pattern has to be escaped (to distinguish it from the comments) but we don't have these here. The pattern can thus be re-written as follows (the result is the same):

if ($line =~ / (?<!       # huh? (?) ahh, look left (<) for something
                          # NOT (!) appearing on the left.
                ^)        # oh, ok, I got it, there must be no '^' on the left
                KEYWORD:  # but the string 'KEYWORD:' should come then
             /x ) {
    # found KEYWORD in '$line', but not at the beginning
    print $line, "\n";
}

Upvotes: 3

Avikd
Avikd

Reputation: 166

Another simple regex

print if /^.+KEYWORD/;

match

hello this is me. KEYWORD: blah

Upvotes: 1

Borodin
Borodin

Reputation: 126722

I think you need to give better, real examples

On the face of it, all you need is

if ( /KEYWORD/ and not /^KEYWORD/ ) {
     ...
}

Upvotes: 1

Dada
Dada

Reputation: 6626

If you are just looking for a keyword, you should be using index and not a regex :

if (index($line, 'KEYWORD') > 0) {
    # do something
}

See the documentation : index STR, SUBSTR returns -1 if SUBSTR isn't found in STR, otherwise it return the index of SUBSTR in STR (starting at 0).

If you want to look for a more complex pattern than a simple keyword, then you should do as @Perl Dog said in his answer.

Upvotes: 4

Related Questions