简体   繁体   English

获取特定字符后的数字 C#

[英]Get numbers after a specific character C#

I have a string containing this text...我有一个包含此文本的字符串...

1. G66I11.J270.P5.C90.(+K2H1+)
2. G66I11.J90.P-5.(+K2H1+)
3. G66I215.4J270.P-55.Q-6.T2531(+K2H1+)
...

I need to extract the value after character "P" is 5 or 55.我需要提取字符“P”为 5 或 55 之后的值。

Now I'm using IndexOf to get:现在我使用 IndexOf 来获取:

int indexP = 0;
int number;
if (lines.Contains("P-"))
{
     indexP = lines.IndexOf("P-") + 1;
}
else if (lines.Contains("P") && !lines.Contains("P-"))
{
     indexP = lines.IndexOf("P");
}
if (lines.Contains("Q"))
{
    int indexQ = 0;
    if (lines.Contains(".Q"))
    {
         indexQ = lines.IndexOf(".Q");
    }

    if (indexQ > indexP)
    {
          number = Int.Parse(lines.Substring(indexP + 1, indexQ - indexP - 1));
    }
}

if (lines.Contains("C"))
{
    int indexC = 0;
    if (lines.Contains(".C"))
    {
         indexC = lines.IndexOf(".C");
    }

    if (indexC > indexP)
    {
          number = Int.Parse(lines.Substring(indexP + 1, indexC- indexP - 1));
    }
}
...

I returned exactly but after "P" character can be any character.我完全返回,但在“P”字符之后可以是任何字符。

So if do it this way it will be very long code:(因此,如果这样做,它将是很长的代码:(

i want to find a shorter way.我想找到一条更短的路。 Can you show me how to do something?你能告诉我怎么做吗? Thanks.谢谢。

You could use a Regex and match expression.您可以使用正则Regex和匹配表达式。 Something like this,像这样的东西,

// Example input
List<string> input = new List<string>();
input.Add("G66I11.J270.P5.C90.(+K2H1+)");
input.Add("G66I11.J90.P-5.(+K2H1+)");
input.Add("G66I215.4J270.P-55.Q-6.T2531(+K2H1+)");
input.Add("G66I11.J90.X-5.(+K2H1+)");

Regex match = new Regex(@"(?<=P)-*\d+(?=.)");
var values = input.Select(x => match.Match(x)?.Value).Where(x => !string.IsNullOrEmpty(x)).ToList();

// values =
Count = 3
    [0]: "5"
    [1]: "-5"
    [2]: "-55"

Regex(@"(?<=P)-*\d+(?=.)"); -> this checks for P from the start.. and after finding it, it takes 0 or more - .. takes the number (\d+) until there is a . -> 这从一开始就检查P .. 找到它之后,它需要 0 或更多- .. 取数字 (\d+) 直到有. . .

Only caveat to this is.. it only selects the first match.唯一需要注意的是..它只选择第一个匹配项。 If you want multiple matches, switch the match.Match to match.Matches method which gives you a list of values.如果您想要多个匹配项,请将match.Match切换为match.Matches方法,该方法会为您提供值列表。 you'll have to update the Select statement to return all values.您必须更新 Select 语句以返回所有值。

Try this - assuming your 'lines' is a multi-line string like so.试试这个 - 假设你的 'lines' 是一个像这样的多行字符串。

string lines = @"
1. G66I11.J270.P5.C90.(+K2H1+)
2. G66I11.J90.P-5.(+K2H1+)
3. G66I215.4J270.P-55.Q-6.T2531(+K2H1+)
";
Regex regex = new Regex(@"[^P]*P-?([^\.]+)\.");

var matches = regex.Matches(lines);
Console.WriteLine($"Count: {matches.Count}");
foreach (Match match in matches)
{
    Console.WriteLine($"{match.Groups[1].Value}");
}

// Count: 3
// 5
// 5
// 55

For a single line:对于单行:

Regex regex = new Regex(@"[^P]*P-?([^\.]+)\.");

string line = "1. G66I11.J270.P5.C90.(+K2H1+)";
var match = regex.Match(line);
Console.WriteLine($"{match.Groups[1].Value}");
// 5

You could use a pattern to match P followed by an optional - and then capture the numbers 0-9 in a group.您可以使用模式来匹配 P 后跟可选-然后捕获一组中的数字 0-9。

\bP-?([0-9]+\b
  • \bP-? A word boundary, match P and optional -一个单词边界,匹配P和可选-
  • ([0-9]+) Capture 1 or more digits 0-9 in group 1 ([0-9]+)捕获组 1 中的 1 个或多个数字 0-9
  • \b A word boundary to prevent a partial word match \b防止部分单词匹配的单词边界

See a .NET regex demo (click on the table tab) and a C# demo .请参阅.NET 正则表达式演示(单击表格选项卡)C# 演示

For example例如

string pattern = @"\bP-?([0-9]+)\b";
string s = @"1. G66I11.J270.P5.C90.(+K2H1+)
2. G66I11.J90.P-5.(+K2H1+)
3. G66I215.4J270.P-55.Q-6.T2531(+K2H1+)";
var numbers = Regex.Matches(s, pattern)
           .Select(i => int.Parse(i.Groups[1].Value))
           .ToArray();
Console.WriteLine("[{0}]", string.Join(", ", numbers));

Output Output

[5, 5, 55]

I did it Jawad's way我按照贾瓦德的方式做到了

But it not match for the string have "P" at the final string, example: G98X30.Y292.5I87.75J18.5P5但它不匹配最后一个字符串有“P”的字符串,例如:G98X30.Y292.5I87.75J18.5P5

So i added * to the final regex:所以我在最终的正则表达式中添加了 *:

Regex match = new Regex(@"(?<=P)-*\d+(?=.)*");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM