Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Finding string literals in my code

Tags:

regex

php

At my company we recently noticed that one developer was not using language files but putting text directly in the code.

My idea was to search for words between quotes that have atleast 1 or more whitespace in them. But I got kinda stuck with

("|')(\w|\s{1,})*('|")

this does match text but does not require that it has atleast 1 word and atleast 1 whitespace (so it matches about anything between quotes). Can anyone help me out?

The language I want to use for this is PHP (or I could do a notepad++ search)

like image 604
Rob Avatar asked Jan 28 '12 15:01

Rob


1 Answers

If you want to match single or double quoted strings (without escapes) that contain a "word" and a space you could use:

"(?=[^"\n]*\w)(?=[^"\n]*\s)[^"\n]+"|'(?=[^'\n]*\w)(?=[^'\n]*\s)[^'\n]+'

In PHP it would look like:

preg_match_all("/\"(?=[^\"\n]*\\w)(?=[^\"\n]*\\s)[^\"\n]+\"|'(?=[^'\n]*\\w)(?=[^'\n]*\\s)[^'\n]+'/", $string, $matches);
like image 189
Qtax Avatar answered Oct 10 '22 09:10

Qtax