5

I'm using NetBeans and I want to know what regular expression to use to add single/double quotes around each constant. Every constant is defined like this:

define(SYSTEM_BASEDIR, '/base/dir');

Afaik, that is not the correct way. I need to convert all constants to this:

define('SYSTEM_BASEDIR', '/base/dir');

Thanks in advance to all helpers!

Stylock
  • 1,459
  • 2
  • 12
  • 14

6 Answers6

9

You are correct that define(SYSTEM_BASEDIR, '/base/dir'); is invalid syntax since you are using the constant before defining it.

Now for the regex:

Open up the Replace Dialog (Ctrl+H)

Find What: define\((\w*),

Replace With: define("$1",

This will turn this:

define(BLA,"test");

into:

define("BLA","test");
Gung Foo
  • 13,392
  • 5
  • 31
  • 39
4

Netbeans Ctrl+H

find: (define\()(\w*)(\,)
replace: define("$2",

check Regular expression

Ayed Mohamed Amine
  • 587
  • 2
  • 10
  • 30
2
$result = preg_replace('/\bdefine\((\w+),/', 'define(\'\1\',', $subject);

changes all instances of

define(<alphanumeric word>,

into

define('<alphanumeric word>',
Tim Pietzcker
  • 328,213
  • 58
  • 503
  • 561
  • @GungFoo: Stylock asked how to replace all constants in PHP, he tagged the question PHP, and this is a Q&A site for programming questions. So I naturally assumed he was looking for a PHP solution. – Tim Pietzcker Feb 07 '13 at 09:31
2

Netbeans Ctrl+H

find: define\(([A-Za-z_]+),
replace: define('$1',

check Regular expression

Naveed S
  • 5,106
  • 4
  • 34
  • 52
Norton
  • 340
  • 1
  • 3
1

From your editor find replace text

use this reg expression there :

find : define\((.*),

replace : define('\$1',

If your are using notepad++

replace : define('\1', 
Prasanth Bendra
  • 31,145
  • 9
  • 53
  • 73
  • @NaveedS : Have you tried it? it works for me in notepad++ (I am not sure about net beans, but i guess it will work) – Prasanth Bendra Feb 07 '13 at 09:32
  • `\1` can't be used for capture group reference in replacement string, but it can be used for backreference inside the regex. It worked in Notepad++ due to difference in the supported regex standard. Check this http://stackoverflow.com/questions/11970405/notepad-regex-backreference-syntax-in-search-replace-1-or-1 – Naveed S Feb 07 '13 at 09:50
0

replace using this regex

(?<constant>(?:[A-Z]+(_[A-Z]+)*))
Lorcan O'Neill
  • 3,303
  • 1
  • 25
  • 24