2

I'm a newcomer to regular expressions, and am having a hard time with what appears to be a simple case.

I need to replace "foo bar" with "fubar", where there is any amount and variety of white space between foo and bar.

For what it's worth, I'm using php's eregi_replace() to accomplish this.

Thanks in advance for the help.

mickmackusa
  • 43,625
  • 12
  • 83
  • 136
Lasoldo Solsifa
  • 375
  • 3
  • 5
  • 14

5 Answers5

8

... = preg_replace('/foo\s+bar/', 'fubar', ...);

drdaeman
  • 11,159
  • 7
  • 59
  • 104
  • 3
    Oh, and if you want to replace 'foobar' too, you have to use \s* instead of \s+. "*" means "zero or more times" (the "*" symbol came directly from http://en.wikipedia.org/wiki/Kleene_star), and "+" means "one or more time". "\s" stands for "any whitespace character". Plain unescaped alphanumerics have no special meaning. – drdaeman Jun 11 '09 at 17:44
  • 2
    It is a good idea to use preg_* (like drdaeman has used) instead of ereg_*. The former uses perl-compatible regular expressions (PCRE), which are the de facto standard now and far-better documented on the webs. – Richard Simões Jun 11 '09 at 17:46
  • This expression performs exactly as I'd hoped. Thanks drdaeman, and others, for the help, and for the helpful extra comments. – Lasoldo Solsifa Jun 11 '09 at 18:07
  • Oh, didn't notice you've said ereg*i*. If you need to make regexp case insensitive use "i" flag in regexp: preg_replace('/foo\s+bar/i', ...) – drdaeman Jun 11 '09 at 18:12
  • wouldn't the pattern be better if it looked more like this? ... = preg_replace( '/(\w+)\s+(\w+)/', '\\1\\2', ...); – KOGI Jun 11 '09 at 21:58
  • Well, I understood it that he needs to replace "foo bar" with "fubar" (not "foobar", which your code would do). So, that's exactly what I wrote. – drdaeman Jun 11 '09 at 22:05
1

I'm not sure about the eregi_replace syntax, but you'd want something like this:

Pattern: foo\s*bar
Replace with: fubar
John G
  • 3,483
  • 1
  • 23
  • 12
0

To ensure that you are making whole word matches on foo and bar, it is critical to add word boundaries. \s+ will match one or more whitespaces.

Code: (Demo)

$strings = [
    'foo barf',
    "foo bar",
    "kungfoo bar"
];

var_export(preg_replace('/\bfoo\s+bar\b/', 'fubar', $strings));

Output:

array (
  0 => 'foo barf',
  1 => 'fubar',
  2 => 'kungfoo bar',
)
mickmackusa
  • 43,625
  • 12
  • 83
  • 136
0

Try this:

find = '(foo)\s*(bar)'
replace = '\\1\\2'

\s is the metachar for any whitespace character.

JoshKraker
  • 373
  • 1
  • 5
  • 15
  • 1
    Good example on using capturing brackens, but Lasoldo Solsifa wants to replace "foo bar" with "fubar", not "foobar". – drdaeman Jun 11 '09 at 17:48
0

I also prefer preg_replace, but for completeness, here it is with ereg_replace:

$pattern     = "foo[[:space:]]+bar";
$replacement = "fubar";
$string = "foo    bar";
print ereg_replace( $pattern, $replacement, $string);

that prints "fubar"

artlung
  • 33,305
  • 16
  • 69
  • 121
  • 1
    Actually, $pattern = "foo[[:space:]]+bar"; $replacement = "fubar";. Otherwise it will happily replace "foo baz" too, which is probably not something we want. – drdaeman Jun 11 '09 at 18:09