0

I'm trying to build a regex that will use preg_replace to quote the right side of an expression if it's unquoted.

So myvar = 3 becomes myvar = '3'. It should only deal with unquoted strings that are contiguous (so if there any spaces on the first string need be quoted e.g. myvar = 3 5 will become myvar = '3' 5).

I also want it to ignore any quoted string, so myvar = 'this is quoted' should not be modified.

So far I have the code below:

$str = 'myvar = 3';
$regex = '/([\w\@\-]+) *(\=|\>|\>\=|\<|\<\=) *([\w]+)/i';
$replace = '\1 \2 \'\3\'';
$result = preg_replace($regex, $replace_str, $str);

What do I need to put in $regex to make this work?

stema
  • 90,351
  • 20
  • 107
  • 135
ObiHill
  • 11,448
  • 20
  • 86
  • 135
  • 2
    This is your fifth regex question in row; all seem to pertain to the same task of dealing with JS expressions of some sort. And it looks like you're depending on Stackoverflow to iteratively improve something you have too little experience with. Maybe it's time to learn it more in-deep? * See also [Open source RegexBuddy alternatives](http://stackoverflow.com/questions/89718/is-there) and [Online regex testing](http://stackoverflow.com/questions/32282/regex-testing) for some helpful tools, or [RegExp.info](http://regular-expressions.info/) for a nicer tutorial. – mario Dec 29 '12 at 23:02
  • @mario Thanks for the resources, I'll take a look at them. I can't tell you how much I've learned from the responses I've gotten here on stackoverflow. Being I'm not a complete noob, I just need some direction. Usually, when you get a good response on the best approach, it makes it easier for you to know what to focus on learning going forward. – ObiHill Dec 29 '12 at 23:14
  • http://stackoverflow.com/a/4209320/1533203 That what you're looking for? – David Harris Dec 29 '12 at 23:17
  • @DavidHarris Thanks. I think it's something I can rejig for what I need to do. – ObiHill Dec 29 '12 at 23:22

2 Answers2

0

You could use preg_replace_callback, or maybe this could work (right side of the regex only):

#([^\'\"])(\w+)([^\'\"])#

And replace like this:

$exp = '#([^\'\"])(\w+)([^\'\"])#';
$str = 'This is unquoted';
$quoted = preg_replace($exp, '\'$1$2$3\'', $str); // should hopefully be 'This is unquoted' after. Not tested.

It's kind of a hacky work-around though.

EDIT: Yeah, no-go. I'd recommend preg_replace_callback then.

David Harris
  • 2,697
  • 15
  • 27
  • The string in `$str` should be an expression e.g. `$str = 'myvar = 3'`. I'm trying to quote the string after the sign of the expression – ObiHill Dec 29 '12 at 23:20
  • Yes I know, that's why I said "it's only the right side of the regex" :) But this doesn't work anyway :( Sorry, I tried! – David Harris Dec 29 '12 at 23:22
0

I eventually settled for the following:

$str = 'myvar = 3';
$regex = '/([\w\@\-]+) *(\=|\>|\>\=|\<|\<\=) *([^\s\']+)/i';
$replace = '\1 \2 \'\3\'';
$result = preg_replace($regex, $replace_str, $str);

I needed this as one step of a multi-step operation so I wanted to use this regex in question to bake an uber regex that would do everything in one go. However, it turns out preg_replace can also be fed 'search' and 'replace' arrays, so I went with that for my final solution. It's much easier to grasp that way as well.

Cheers.

ObiHill
  • 11,448
  • 20
  • 86
  • 135