Regular Expression Query



Hi,

I have a string in my program which can contain all parts of an address. If
any part of the address is missing, it still puts in the trailing comma - so
an address can look like

The Big House, Long Lane,,,London,,E1C 1VA

I want to run a regular expression on this to strip out the duplicate
commas. Visiting http://www.design215.com/toolbox/regexp.php, I worked out
that the expression I want is

/(,){2,}/g,", "

('worked it out' = it works on the tester on that site!)

I want to use this expression in code, and have tried

$longstring = "The Big House, Long Lane,,,London,,E1C 1VA"
$pattern = '/(,){2,}/g,", "';
ereg($pattern, $longstring, $shortstring);
echo "[".$shortstring[0]."]";

But nothing is displayed.

Can anyone point out where I'm going wrong?

Regards,

Pete.


.



Relevant Pages

  • Re: Regular Expression Query
    ... I have a string in my program which can contain all parts of an address. ... any part of the address is missing, it still puts in the trailing comma - so ... Rik Wasmus ...
    (alt.php)
  • Re: Printing a "status " line from a python script
    ... Chris Seymour wrote: ... directory and search in the different files for a specific string. ... What my colleague wants is that when ... this with printing without line endings using a trailing comma: ...
    (comp.lang.python)
  • Re: Two problems around firstrightmark
    ... > namely to eliminate a trailing comma from a string, ... braces around the argument. ... Prev by Date: ...
    (comp.text.tex)
  • Re: Converting a list join to a scaler/string
    ... > The end goal is to build an SQL INSERT: ... And that's what you'll get (well you want get the parenthesis and you'll ... have a trailing comma - which can be fixed by moving the comma to the ... I can't see how that is not an "actual string". ...
    (comp.lang.perl.misc)