Regex to split a string

Hi Guys,

I am currently working on ripping some information from a text file. I am trying to use regex to split up some strings.

I.e. text file has this line

Serial Number .............................. ABC123456AB

I need to just get the "ABC123456AB" part. So far I have not been able to work out the regex for it. I would like to search for " Serial Number .............................. " and return just the AB123456AB part.

Cheers

Wilson



Answer this question

Regex to split a string

  • js06

    Following code snippet may help:

     		
    private void btnRegExTest_Click(object sender, System.EventArgs e)
    {
    	
    	Regex matcher 
    		= new Regex(@"[^Serial][^Number]\s\w*", 
    				RegexOptions.Multiline | RegexOptions.Compiled);
    
    	string str2Match 
    			= "Serial Number .............................. ABC123456AB";
    
    	MatchCollection matches = matcher.Matches(str2Match);
    	if (matches != null && matches.Count > 0)
    	{
    		foreach (Match match in matches)
    		{
    			MessageBox.Show(match.ToString().Replace(".", string.Empty).Trim());
    		}
    	} 
    	else
    	{
    		MessageBox.Show("No match found");
    	}
    
    } 
     

    Cheers


  • hehemouse

    Hi,

    try it with the following regexp:

    text = "Serial Number .............................. ABC123456AB";
    Match match1 = Regex.Match(text, "( <=Serial Number\\s+\\.+\\s+).*");

    Andrej



  • Regex to split a string