Help with Regex.Replace pls... 
Author Message
 Help with Regex.Replace pls...

Hi,

I have some numbers validated like this:
^\d{2,4}-[1-9]\d{4}(\d{0,3}$
How can I using the replace method add a space between each pair of digits
after the - sign?
Like this: "##-## ## #" or "##-## ## ##" or
"##-## ## ## #" or "##-## ## ## ##" etc.

Using the code below I can do that but if group 5 or 6 are not matched and
extra " " is always added to the end of the number e.g. "##-## ## # ".
objRegEx.Replace(strNumber, "^(\d{2,4}-[1-9]\d)(\d\d)(\d)(\d?)(\d?)(\d?)$",
"$1 $2 $3$4 $5$6"))

There should be a much better way of doing this using regular expression
replace, but I don't know how...
--
Thanks in advance
Ali Eghtebas Sweden



Sun, 13 Nov 2005 07:28:07 GMT  
 Help with Regex.Replace pls...
Ok this is how I solved this problem, to let others benefit from the
solution...

    Public Function FormatSePhoneNumber(ByVal strPhoneNumber As String) As
String
        Dim objRegEx As Regex 'For pattern matching using regular
expression.
        'MatchEvaluator delegate. An event handler that fires when an
"OnMatch event" occurs.
        Dim objMatchEval As New MatchEvaluator(AddressOf MatchHandler)

        Try
            Return objRegEx.Replace(strPhoneNumber,
"(?<=-(?:\d{2}|\d{4}|\d{6}))(\d)", objMatchEval)
        Catch ex As Exception
            'If any exceptions are thrown.
            MsgBox(ex.ToString, MsgBoxStyle.Critical, "Error")
            Return strPhoneNumber 'No changes.
        End Try
    End Function

    Private Function MatchHandler(ByVal objMatch As Match) As String
        Try
            Return " " & objMatch.Groups(1).Value 'Add a space before the
matched group.
        Catch ex As Exception
            'If any exceptions are thrown.
            MsgBox(ex.ToString, MsgBoxStyle.Critical, "Error")
            Return objMatch.Groups(1).Value 'No changes.
        End Try
    End Function

--
Regards
Ali Eghtebas Sweden

Quote:
> Hi,

> I have some numbers validated like this:
> ^\d{2,4}-[1-9]\d{4}(\d{0,3}$
> How can I using the replace method add a space between each pair of digits
> after the - sign?
> Like this: "##-## ## #" or "##-## ## ##" or
> "##-## ## ## #" or "##-## ## ## ##" etc.

> Using the code below I can do that but if group 5 or 6 are not matched and
> extra " " is always added to the end of the number e.g. "##-## ## # ".
> objRegEx.Replace(strNumber,

"^(\d{2,4}-[1-9]\d)(\d\d)(\d)(\d?)(\d?)(\d?)$",
Quote:
> "$1 $2 $3$4 $5$6"))

> There should be a much better way of doing this using regular expression
> replace, but I don't know how...
> --
> Thanks in advance
> Ali Eghtebas Sweden



Mon, 14 Nov 2005 17:57:20 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. RegEx Special Replacing (not the standard .replace)

2. Using the RegEx Classes to replace text really fast

3. Anchored Regex search and replace question

4. RegEx replace

5. Problem connecting to Access2000 database...pls, pls help.

6. Give me some hints pls, Help pls

7. Help pls, Give me some hints pls

8. PLS HELP!!! PLS HELP!!! PLS HELP!!!

9. Replace and String.Replace only replaces first occurrence?

10. Help w/Regex?

11. REGEX Help

12. need help with Regex Expression using VB6

 

 
Powered by phpBB® Forum Software