Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex match a line without a specific word

Tags:

regex

I want to use regex to highlight the functions of a new programming language but i have a problem in EXCLUDING the functions that have the word "exported" so

ok lines that I have to match examples:

routine hello
ROUTINE hello
   routine hello
   ROUTINE hello(a:INTEGER)
   routine hello (a   :  INTEGER)
   routine hello (a   :  INTEGER , b: STRING)

lines that I don't want to match examples:

   routine hello (a   :  INTEGER , b: STRING) exported

I've tried with

^[[:blank:]]*routine[[:blank:]]+([[:alnum:]_])+[[:blank:]]*([[:alnum:]_:,[:space:]]*)/^(?!.*exported)$/

but it doesn't work.

like image 821
Dedanan Avatar asked May 15 '13 10:05

Dedanan


People also ask

How do you match a blank line in regex?

The most portable regex would be ^[ \t\n]*$ to match an empty string (note that you would need to replace \t and \n with tab and newline accordingly) and [^ \n\t] to match a non-whitespace string. Save this answer.

What does '$' mean in regex?

$ means "Match the end of the string" (the position after the last character in the string). Both are called anchors and ensure that the entire string is matched instead of just a substring.

How do I remove a specific character from a string in regex?

If you are having a string with special characters and want's to remove/replace them then you can use regex for that. Use this code: Regex. Replace(your String, @"[^0-9a-zA-Z]+", "")

What is wildcard regex?

Wildcard which matches any character, except newline (\n). | Matches a specific character or group of characters on either side (e.g. a|b corresponds to a or b) \ Used to escape a special character.


2 Answers

You can use an expression like this to match all lines that don't contain the word "exported":

(?m)^(?!.*\bexported\b).*$
like image 166
Qtax Avatar answered Oct 10 '22 17:10

Qtax


The following expression also does the trick:

^(?!.*exported).*$

|________________   line begin 
 |_______________   negative lookahead
    |____________   any characters
      |__________   your exclusion word
               |_   any characters
                 |_ end of line
like image 44
DrMarbuse Avatar answered Oct 10 '22 15:10

DrMarbuse