Re: Regular Expression Query



P <mailnotanswered@xxxxxxxxxxxxxxxxxxxx> wrote:

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?

Hmmz, I'd use PCRE:

$shortstring = preg_replace('/,+/',', ',$longstring);

--
Rik Wasmus
* I'm testing several new newsreaders at the moment. Please excuse possible errors and weird content. *
.



Relevant Pages

  • 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 ...
    (alt.php)
  • Problems Managing Required Field and Form Events
    ... Dim CheckVal As String ... If CheckVal = "Missing" Then ... Private Sub Form_BeforeUpdate ... Do you want to close the database object anyway? ...
    (microsoft.public.access.formscoding)
  • Problems Managing Required Field and Form Events
    ... Dim CheckVal As String ... If CheckVal = "Missing" Then ... Private Sub Form_BeforeUpdate ... Do you want to close the database object anyway? ...
    (microsoft.public.access.formscoding)
  • Re: Unicode question
    ... You're missing the existence and continued use of UTF-8 and UTF-16. ... I want to use an extended string that's good enough to cover UCS-4 ... sometimes multiple characters must be treated as one unit. ...
    (borland.public.delphi.non-technical)
  • A Problem Using Hashes
    ... I am inching my way through INTERMEDIATE PERL and tried using hashes to ... Use of uninitialized value in string eq at chap4-take6.pl line 49. ... gilligan is missing lotion ... professor is missing coat ...
    (perl.beginners)