.NET Framework Bookmark and Share   
 index > Regular Expressions > regex pattern for the string
 

regex pattern for the string

i have line like this

18N694 P321 117461, 50374 50MIL;

XTAL_X1 P253 116711, 66524 39MIL;

HR_ICH_HUB_CLK66_R P1567 109761, 68424 100MIL;

P64H2_CLK66_R P2177 110161, 68424 75MIL;


I want to sort the lines in accending order according to the value infront the word MIL. Like i want all the line of 39MIL first followed by 50MIL and so on.. I want to use TComparer. I need a regex for values 39MIL,50MIL,75MIL and 100MIL. Can any one give me the correct regex pattern

  • Changed Typekayatri Tuesday, September 08, 2009 7:12 AM
  •  
kayatri
Yes, conversion to VB is not easy when delegates are involved. This may not be the best conversion, I am not as strong as I'd like in VB.Net...

Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim strlist As New List(Of String)
        strlist.Add("18N694 P321 117461, 50374 50MIL;")
        strlist.Add("XTAL_X1 P253 116711, 66524 39MIL;")
        strlist.Add("HR_ICH_HUB_CLK66_R P1567 109761, 68424 100MIL;")
        strlist.Add("P64H2_CLK66_R P2177 110161, 68424 75MIL;")

        strlist.Sort(AddressOf MyCompare)
        For Each s In strlist
            Console.WriteLine("{0}", s)
        Next
    End Sub
    Private Function MyCompare(ByVal s1 As String, ByVal s2 As String) As Integer
        Dim pattern As String = "(?<=\s)[^\s]*(?=MIL)"
        Dim val1 As String, val2 As String
        val1 = Regex.Match(s1, pattern).Value
        val2 = Regex.Match(s2, pattern).Value
        Dim i1 As Integer, i2 As Integer
        i1 = Convert.ToInt32(IIf(String.IsNullOrEmpty(val1), "0", val1))
        i2 = Convert.ToInt32(IIf(String.IsNullOrEmpty(val2), "0", val2))
        MyCompare = i1.CompareTo(i2)
    End Function

End Class

Les Potter, Xalnix Corporation, Yet Another C# Blog
  • Marked As Answer bykayatri Thursday, September 10, 2009 1:13 AM
  •  
xalnix

string pattern = @" [0-9]*MIL";

string testData = "XTAL_X1 P253 116711, 66524 39MIL";

Regex rx = new Regex(pattern);

Console.WriteLine(testData.Substring(rx.Match(testData).Index + 1));

Is This what you wanted??

  • Edited byparas kumar Tuesday, September 08, 2009 12:25 PMformatting
  • Edited byparas kumar Tuesday, September 08, 2009 12:27 PMformatting
  • Edited byparas kumar Tuesday, September 08, 2009 12:29 PMElaborate
  • Edited byparas kumar Tuesday, September 08, 2009 12:31 PMAdding text
  • Proposed As Answer byparas kumar Tuesday, September 08, 2009 12:27 PM
  •  
paras kumar
The following code uses a Regex pattern that returns only the value preceding the MIL keyword. That value is then plugged into Convert.ToInt32() to get an integer value to compare. By comparing as integer, 100 comes after 75, but if compared as string, 100 would come before 39.

            string[] strlist = {
                "18N694 P321 117461, 50374 50MIL;",
                "XTAL_X1 P253 116711, 66524 39MIL;",
                "HR_ICH_HUB_CLK66_R P1567 109761, 68424 100MIL;",
                "P64H2_CLK66_R P2177 110161, 68424 75MIL;",
                              };
            string pattern = @"(?<=\s)[^\s]*(?=MIL)";
            List<string> lst = new List<string>();
            lst.AddRange(strlist);
            lst.Sort(new Comparison<string>(delegate(string s1, string s2)
                {
                    string val1, val2;
                    val1 = Regex.Match(s1, pattern).Value;
                    val2 = Regex.Match(s2, pattern).Value;
                    int i1, i2;
                    i1 = Convert.ToInt32(String.IsNullOrEmpty(val1) ? "0" : val1);
                    i2 = Convert.ToInt32(String.IsNullOrEmpty(val2) ? "0" : val2);
                    return i1.CompareTo(i2);
                }));
            foreach (string str in lst)
                Console.WriteLine("{0}", str);


Les Potter, Xalnix Corporation, Yet Another C# Blog
xalnix
Thanx for reply xalnix.

but im using vb.net not c#. Can you provide code in vb. because i tryed to convort but can not
kayatri
Yes, conversion to VB is not easy when delegates are involved. This may not be the best conversion, I am not as strong as I'd like in VB.Net...

Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim strlist As New List(Of String)
        strlist.Add("18N694 P321 117461, 50374 50MIL;")
        strlist.Add("XTAL_X1 P253 116711, 66524 39MIL;")
        strlist.Add("HR_ICH_HUB_CLK66_R P1567 109761, 68424 100MIL;")
        strlist.Add("P64H2_CLK66_R P2177 110161, 68424 75MIL;")

        strlist.Sort(AddressOf MyCompare)
        For Each s In strlist
            Console.WriteLine("{0}", s)
        Next
    End Sub
    Private Function MyCompare(ByVal s1 As String, ByVal s2 As String) As Integer
        Dim pattern As String = "(?<=\s)[^\s]*(?=MIL)"
        Dim val1 As String, val2 As String
        val1 = Regex.Match(s1, pattern).Value
        val2 = Regex.Match(s2, pattern).Value
        Dim i1 As Integer, i2 As Integer
        i1 = Convert.ToInt32(IIf(String.IsNullOrEmpty(val1), "0", val1))
        i2 = Convert.ToInt32(IIf(String.IsNullOrEmpty(val2), "0", val2))
        MyCompare = i1.CompareTo(i2)
    End Function

End Class

Les Potter, Xalnix Corporation, Yet Another C# Blog
  • Marked As Answer bykayatri Thursday, September 10, 2009 1:13 AM
  •  
xalnix

You can use google to search for other answers

Custom Search

More Threads

• How to stop after countdown
• email address
• Matching at end of string question.
• Very basic question
• Why the regex doesn't catch "Extra" group?
• Validating Guid string before passing to Guid constructor
• Please help on Regular Expression
• simple regex
• regular expression-- needed help badly
• a regex for single consonants