Results 1 to 8 of 8

Thread: [RESOLVED] [2005] Reg Exp help.

  1. #1

    Thread Starter
    Frenzied Member Asgorath's Avatar
    Join Date
    Sep 2004
    Location
    Saturn
    Posts
    2,036

    Resolved [RESOLVED] [2005] Reg Exp help.

    Hi.
    I need a regular expression to search a string that contains the possible combinations.
    C1..C9
    D1..D24

    Example 1 "C1D2C9"
    Example 2 "C1D1D2D6D11" maximum number of 5 combinations.

    Thanks in advance.

    Jorge.
    "The dark side clouds everything. Impossible to see the future is."

  2. #2
    Hyperactive Member
    Join Date
    Sep 2006
    Location
    India
    Posts
    310

    Re: [2005] Reg Exp help.

    hi,
    try this
    '===========
    ([C-D][0-9]){0,5}
    '----------
    sagar

  3. #3

    Thread Starter
    Frenzied Member Asgorath's Avatar
    Join Date
    Sep 2004
    Location
    Saturn
    Posts
    2,036

    Re: [2005] Reg Exp help.

    I am new to regexp but t though that that .Match class would return all the positions where a match is found but it only returns the 1st position! I have to browse the string myself?
    Jorge
    "The dark side clouds everything. Impossible to see the future is."

  4. #4
    Addicted Member Isorfir's Avatar
    Join Date
    Feb 2006
    Location
    In Debug Mode
    Posts
    133

    Re: [2005] Reg Exp help.

    You have to loop through the matches:

    VB Code:
    1. Dim matchList As New ArrayList
    2.       Dim strText As String = "C1D1D2D6D11"
    3.  
    4.       Dim Regex As New Regex("([C-D][0-9]){1,5}", RegexOptions.IgnoreCase)
    5.       Dim Mymatches As MatchCollection = Regex.Matches(strText)
    6.       For Each FoundMatch As Match In Mymatches
    7.             matchList.Add(FoundMatch.Value)
    8.       Next
    IDE: Visual Studio 2008/2010 - C# | VB.Net

  5. #5

    Thread Starter
    Frenzied Member Asgorath's Avatar
    Join Date
    Sep 2004
    Location
    Saturn
    Posts
    2,036

    Re: [2005] Reg Exp help.

    Hi, there's a litttle problem i tried this
    VB Code:
    1. Dim matchList As New ArrayList
    2.                         Dim strText As String = "D20D21D22D23D24"
    3.  
    4.                         Dim Regex As New System.Text.RegularExpressions.Regex("([C-D][0-9]){1,5}")
    5.                         Dim Mymatches As System.Text.RegularExpressions.MatchCollection = Regex.Matches(strText)
    6.                         For Each FoundMatch As System.Text.RegularExpressions.Match In Mymatches
    7.                             matchList.Add(FoundMatch.Value)
    8.                             ''matchList.Add(FoundMatch.Index)
    9.                         Next
    10.                         For i = 0 To matchList.Count - 1
    11.                             Console.WriteLine(matchList(i).ToString)
    12.                         Next
    It outputs
    D2
    D2
    D2
    D2
    D2
    instead of
    D20
    D21
    D22
    D23
    D24

    Any suggestions.
    Jorge
    "The dark side clouds everything. Impossible to see the future is."

  6. #6
    PowerPoster VBDT's Avatar
    Join Date
    Sep 2005
    Location
    CA - USA
    Posts
    2,922

    Re: [2005] Reg Exp help.

    Do you want the unique combinations? if yes check for it in my signature.

  7. #7
    Addicted Member Isorfir's Avatar
    Join Date
    Feb 2006
    Location
    In Debug Mode
    Posts
    133

    Re: [2005] Reg Exp help.

    Use this search string instead:

    VB Code:
    1. Dim Regex As New Regex("(C[1-9]|(D[1-9])(?!\d)|D1\d|D2[1-4])")

    This will only find C1-9 or D1-24 in the string "C0C1C2D1D3D15D0D20D21D22D23D24D25D30"

    This is assuming you don't want the other numbers.
    Last edited by Isorfir; Oct 19th, 2006 at 01:36 PM.
    IDE: Visual Studio 2008/2010 - C# | VB.Net

  8. #8

    Thread Starter
    Frenzied Member Asgorath's Avatar
    Join Date
    Sep 2004
    Location
    Saturn
    Posts
    2,036

    Re: [2005] Reg Exp help.

    Ok, this search expression works pefectly, Thanks.
    Jorge
    "The dark side clouds everything. Impossible to see the future is."

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