-1

Say that I have this text

I need to think of something to write here. I am an alumni of the college. The blue cat is blue the red cat is red.

I want to be able to select everything if the word "student" (ignoring case) does NOT exist in the text. Therefore, I need something to go beyond the first line. I originally had something like

/(?:[^student]).*/ 

but it isn't working correctly. I am not sure what "flavor" of Regex I am using but it is with PHP and in the backend of a Drupal site.

Thank you!

Mona NM
  • 1
  • 3

1 Answers1

0

Problem

As suggested by ctwheels comment, the regexp you have written (?:[^student]).* is not doing what you think it does. Lets break down your attempt to illustrate the problem:

(?: ) <--- this part is a non-capturing group

[^student] <--- this part matches a single character not present in the list

.* <--- matches any character (except for line terminators)

I.e, your bracket expression is the same as [^tneduts] or [^neduts]or even [^denstu]. It doesn't look for a word!

Solution

What you want to do instead, is using a negative lookahead. like this ^(?!.*student).*$. Let's break it down.

^ <--- position at start of the string

(?!.*student) <--- look for anything followed by student, don't match if found

.* <--- take everything on the line unless the negative lookahead is in effect.

$ <--– position at the end of the string

jjabba
  • 494
  • 3
  • 16
  • 1
    is there a way I could replace .* in your solution to get it to search and select multiple lines? – Mona NM Feb 07 '18 at 19:01
  • You could try to match newlines explicitly but I avoid regexp for multiline parsing. If you have longer texts I suggest you look for the word 'student' in the entire file instead using a simple line by line character matcher like [strpos()](http://php.net/manual/en/function.strpos.php). – jjabba Feb 07 '18 at 19:20