Skip to main content
 首页 » 编程设计

c#之从字符串请求中获取特定字符

2024年02月05日20落叶无声

我需要从字符串中获取 4 个变量

这是某人可以向服务器发出的请求:

String request= "Jouer_un_bulletin <nbT> <mise> <numeros_grilleA> <numeros_grilleB>" 

我需要获取 nbt、mise、numeros_grilleA 和 numeros_grilleB

请您参考如下方法:

您可以尝试使用正则表达式:

  String request = "Jouer_un_bulletin <nbT> <mise> <numeros_grilleA> <numeros_grilleB>"; 
 
  String pattern = @"(?<=<)[^<>]*(?=>)"; 
 
  String[] prms = Regex 
    .Matches(request, pattern) 
    .OfType<Match>() 
    .Select(match => match.Value) 
    .ToArray(); 

测试:

  // nbT 
  // mise 
  // numeros_grilleA 
  // numeros_grilleB 
  Console.Write(String.Join(Environment.NewLine, prms));