Home > Back-end >  Regex for this formula
Regex for this formula

Time:10-13

I'm struggling with making a regex for this type of formula: LU1O/00054362/8. I'd like to receive only the first part - LU1O every time. It's always letter, letter, number, letter. Please help me with suggestion.

CodePudding user response:

try this:

   string input = @"LU1O/00054362/8";
   var regex = "^[A-Z]{2}[0-9][A-Z]$";
   var match = Regex.Match(input.Substring(0,4), regex, RegexOptions.IgnoreCase);
   if (match.Success)
   {
        //matched
   }

or

  string input2 = @"LU1O/00054362/8";
  var regex2= "^[A-Z]{2}[0-9][A-Z]";
  var match2 = Regex2.Match(input2, regex2, RegexOptions.IgnoreCase);
  if (match2.Success)
  {
      //matched
  }

CodePudding user response:

If '/' is always a delimiter you could string.split('/') and access the first element from the resulting array.

  • Related