【问题标题】:Find String in Text and Extract在文本中查找字符串并提取
【发布时间】:2018-09-12 13:53:42
【问题描述】:

目前我面临一个问题。我有一个字符串列表:

  1. Pferde>Bandagen und Gamaschen>Glocken und Fesselschutz,Marken>Waldhausen,Pferde>Bandagen und Gamaschen
  2. Pferde>Satellzubehör>Zubehör,Marken>Waldhausen,Pferde>Satelzubehör
  3. Pferde>Sättel,Marken>Wintec
  4. Marken>Wintec
  5. Reiter>Reithelme und Sicherheit>Reflexartikel,Pferde>Bandagen und Gamaschen>Glocken und Fesselschutz,Marken>Waldhausen,Reiter>Reithelme und Sicherheit,Pferde>Bandagen und Gamaschen
  6. Pferde>Trensen und Zubehör

我只想要"Marken>" 之后的值。但是,有些行没有"Marken>",并且位置并不总是相同。

编辑:这是我的代码

// Hersteller Fix
string hersteller = "";
string input = tokens[22];


string pattern = @"((.*Marken\>)|(.*?))(?'target'.+)";

RegexOptions options = RegexOptions.Singleline;

foreach (Match m in Regex.Matches(input, pattern, options))
{
    hersteller = m.Index;
    hersteller = m.Groups["target"];
}

这是输出:

{Waldhausen,Pferde>Sattelzubehör}

但我只需要“Waldhausen”、“Wintec”...

你有什么想法吗?

【问题讨论】:

  • 我之前的代码:string[] hname = tokens[22].Split(',');字符串 hersteller = ""; if (hname.Length == 1) { string[] el = tokens[22].Split('>');赫斯特勒 = el[1]; } if (hname.Length == 2) { int index = hname.Length; hersteller = hname[索引-1];字符串[] el = hersteller.Split('>');赫斯特勒 = el[1]; } else if (hname.Length > 2) { int index = hname.Length; hersteller = hname[index -2];字符串[] el = hersteller.Split('>');赫斯特勒 = el[1]; }
  • 发帖前阅读this
  • 获取调试器,在开头放置一个断点并单步执行您的代码。检查你的变量,看看你的程序处于什么状态,并将它与它当时应该处于的状态进行比较。
  • 你想在第一个“Marken>”之后获得价值吗?
  • @Uttam Gupta - 是的,只有第一个值

标签: c# .net string search


【解决方案1】:

这是一种不使用正则表达式的方法,但实际上你应该先尝试一下,然后再在这里提问

var input = "Marken>Wintec";
string mark = "Marken>";

string output = null;

var start = input.IndexOf(mark);
if (start >= 0)
{
    var end = input.IndexOf(",", start);
    if (end >= 0)
    {
        output = input.Substring(start + mark.Length, end - start - mark.Length);   
    }
    else
    {
        output = input.Substring(start + mark.Length);  
    }
}

【讨论】:

    【解决方案2】:
    foreach (var inputLine in inputList)
            {
                if (inputLine.Contains(target))
                {
                    // Get everything in the line after the location of the target word
                    // (indexOf(target) + target.Length ensures we start at the end of the target word, rather than the beginning
                    var result = inputLine.Substring(inputLine.IndexOf(target) + target.Length);
    
                    var indexOfSeparator = result.IndexOf('>');
    
                    if (indexOfSeparator != -1)
                    {
                        // Grab everything from the start of the string to the location of the separator
                        result = result.Substring(0, indexOfSeparator);
                    }
    
                    Console.WriteLine(result);
                }
            }
    

    【讨论】:

      【解决方案3】:

      你可以使用正则表达式:

      using System;
      using System.Text.RegularExpressions;
      
      public class Example
      {
          public static void Main()
          {
              string pattern = @"((.*Marken\>)|(.*?))(?'target'.+)";
              string input = @"Pferde>Bandagen und Gamaschen>Glocken und Fesselschutz,Marken>Waldhausen,Pferde>Bandagen und Gamaschen
      Pferde>Sattelzubehör>Zubehör,Marken>Waldhausen,Pferde>Sattelzubehör
      Pferde>Sättel,Marken>Wintec
      Marken>Wintec
      Reiter>Reithelme und Sicherheit>Reflexartikel,Pferde>Bandagen und Gamaschen>Glocken und Fesselschutz,Marken>Waldhausen,Reiter>Reithelme und Sicherheit,Pferde>Bandagen und Gamaschen
      Pferde>Trensen und Zubehör";
              RegexOptions options = RegexOptions.Multiline;
      
              foreach (Match m in Regex.Matches(input, pattern, options))
              {
                  Console.WriteLine("'{0}' found at index {1}.", m.Groups["target"].Value.Trim(), m.Index);
              }
          }
      }
      

      【讨论】:

      • @cRz 模式本身有效,WriteLine 存在一个小问题。固定。
      【解决方案4】:

      如果它适合你,试试下面的逻辑

       foreach (string x in lst)
                  {
                      int position = x.IndexOf("Marken>");
                      string text = "";
                      if (position > -1)
                      {
                          text = x.Substring(position + 7);
                          break;
                      }
                  }
      

      【讨论】:

        【解决方案5】:
        String str = "Reiter>Reithelme und Sicherheit>Reflexartikel,Pferde>Bandagen und Gamaschen>Glocken und Fesselschutz,Marken>Waldhausen,Reiter>Reithelme und Sicherheit,Pferde>Bandagen und Gamaschen";
        
                        List<String> Splitted = str.Split( ',' ).ToList();
                        foreach ( String Split in Splitted )
                        {
                            if ( Split.Contains("Marken"))
                            {
                                String value = Split.Split( '>' )[1];
                                Console.WriteLine( value );
                                break;
                            }
                        }
        

        结果:瓦尔德豪森

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-02-25
          • 2021-01-13
          • 2018-07-05
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多