Results 1 to 8 of 8

Thread: [RESOLVED] Regex problem

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Jun 2004
    Location
    All useless places
    Posts
    917

    Resolved [RESOLVED] Regex problem

    Hi,

    I've used the below regex expresssion to strip all the characters from a string value and only return the numbers.
    Code:
    String.Join(Nothing, System.Text.RegularExpressions.Regex.Split(strValue, "[^\d]"))
    If the input is: abc12a
    Output is: 12
    The problem is that if I pass abc12.23a then the output is 1223 while I want is 12.23
    Can somebody suggest what should I do?

    Thanks.

  2. #2
    Hyperactive Member Coool's Avatar
    Join Date
    Feb 2006
    Location
    System.Coool
    Posts
    333

    Re: Regex problem

    Well And what if Input is : abc12d34 ???
    I am using .NET 2010 with Windows 7

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Jun 2004
    Location
    All useless places
    Posts
    917

    Re: Regex problem

    Though interesting input, but that won't happen. The reason is that I'm trying to take price out of a string. So price can be $100,234.55MM etc but never in the format that you've specified. And if by any chance my method gets that and computes incorrectly, that won't be an application issue.

  4. #4
    Frenzied Member
    Join Date
    Jun 2005
    Posts
    1,950

    Re: Regex problem

    How about "[0-9]*\.?[0-9]*" or to be more compact "[\d]*\.?[\d]*"

  5. #5
    Frenzied Member bmahler's Avatar
    Join Date
    Oct 2005
    Location
    Somewhere just west of the Atlantic
    Posts
    1,568

    Re: Regex problem

    how bout if you just do a replace on a-z characters?
    VB Code:
    1. System.Text.RegularExpressions.Regex.Replace("abc12.34ac", "[a-zA-Z]*", String.Empty)
    Boooya
    • Visual Studio 2008 Professional
    • Don't forget to use [CODE]your code here[/CODE] when posting code
    • Don't forget to rate helpful posts!
    • If you're question was answered please mark your thread [Resolved]


    Code Contributions:
    PHP
    PHP Image Gallery v1.0PHP Image Gallery v2.0
    VB 2005
    Find Computers on a networkSimple License EncryptionSQL Server Database Access dllUse Reflection to Return Crystal ReportDocumentSilently Print PDFGeneric Xml Serailizer


    Useful Links: (more to come)
    MSDN (The first and foremost)MSDN Design Guidelines API Reference • Inno Setup CompilerInno Setup PreprocessorISTool - Fairly easy to use GUI for creating inno setup projects • Connection StringsNAnt -Automated BuildsCruise Control .NET - Frontend for automated builds

  6. #6
    PowerPoster
    Join Date
    Aug 2005
    Location
    College Station, TX
    Posts
    4,521

    Re: Regex problem

    All you had to do originally is add in the "." to your pattern in your very first post. Since it is a special character, escape it with a slash, so the pattern you needed was just "[^\d\.]"
    VB Code:
    1. Dim strValue As String = "abc12.34abc"
    2. MessageBox.Show(String.Join(Nothing, System.Text.RegularExpressions.Regex.Split(strValue, "[^\d\.]")))

  7. #7

    Thread Starter
    Fanatic Member
    Join Date
    Jun 2004
    Location
    All useless places
    Posts
    917

    Re: Regex problem

    Thanks gigemboy's. That provoked me to read a more about Regex. I think I'll take some more time to get a full hold on it.
    @bmahler's solution won't work on $12.5MM.
    Bulldog, you didn't even try to test the solution you proposed. It strips all the numbers from the string that is passed.
    Thanks to all anyways who took the pain to help me out.

  8. #8
    Frenzied Member
    Join Date
    Jun 2005
    Posts
    1,950

    Re: [RESOLVED] Regex problem

    Bulldog, you didn't even try to test the solution you proposed. It strips all the numbers from the string that is passed.
    What I was showing you was how to match the number sequence you require thinking that you would use this to do a match. So rather than using a "split" which generates an array of segments to sort through, you could use a "match" and generate a match collection. It was tested in RegExBuddy btw.

    You are currently separating out the string sequence you want by removing anything that isnt a "." or a number. This may work today, but for future proofing I would be inclined to match what you "know" to be there, rather than removing the "unknowns" around it. In the future, the database may end up with "Sold,3kilos,at,$125k" or "Note:-Refund,-35$" etc.

    Over time you can easily make a regex match more specific, to deal with negative numbers you can add an optional "-" at the start of the expression, you could also deal with extra spaces, different currency symbols, or the use of a trailing "k" for thousand etc. etc. It would be extremely difficult to do this correctly by removing the characters you dont expect.

    About half of my code is always consumed by error checking, a good mantra being assume nothing! and expect the unexpected!

    Anyway just my $0.02...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width