简体   繁体   中英

search for words that don't start at the the beginning of a line

I only know how to start at the beginning of a line. But I want to exclude the beginning. To be more concrete.

I want to search for the word import. But I don't want things like:

import java.util.*;

As expected this does not work: ^^ for excluding a line start.

Also in a character class ^ has a different meaning so no luck there.

So what would be a good way?

@AndyLester I don't have enough rep to comment on your answer yet but it allows for the string to begin with "import" as long as import is found again in that same string . Using .+ allows for any characters besides newline to come first... including the char sequence "import". So depends what the OP wants to determine if that is a "feature" or "bug"... @clankill3r which takes precedence for your problem 1) not starting with import or 2) containing import somewhere else in the string besides at the start

I think that what you should look to use is a negative lookahead - explained here

To check a string does not start with "import" using regex:

^(?!import)

正则表达式可视化

Debuggex Demo

To check that a string does not start with "import" but does contain "import":

   ^(?!import).*(import).*$

正则表达式可视化

Debuggex Demo

This says "Find me import at the beginning of the line".

^import

So what you're wanting is any line with "import" but there is something else in front of it, right?

^.+import
(?!^)\bimport\b

Try this.This will not find import which is at the beginning.See demo.

https://regex101.com/r/gQ3kS4/1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM