简体   繁体   English

获取特定的字符串模式

[英]get specific string pattern

I have the following string : 我有以下字符串:

string str ="dasdsa|cxzc|12|#dsad#|czxc";

I need a function that return : #dsad# 我需要一个返回的函数: #dsad#

Taking under consideration that #dsad# is created dynamically so it may vary and it's length may vary also 考虑到#dsad#是动态创建的,因此它可能会有所不同,并且其长度也可能会有所不同

How can i do that using regular expression (searching for the string between the two hashtags) or other method if available ? 我如何使用正则表达式(搜索两个主题标签之间的字符串)或其他可用方法来做到这一点?

Assuming you want to match on the # character you can take the following regex: 假设您要匹配#字符,可以使用以下正则表达式:

#[az]+#

  • "[]" character set “[]“ 字符集
  • "+" one character or more “ +”一个或多个字符

If you want your string is always of the form 1|2|3|#4#|5 you could use the String.Split('|') method and just take the fourth element of the result. 如果希望您的字符串始终为1 | 2 | 3 |#4#| 5的形式,则可以使用String.Split('|')方法并仅使用结果的第四个元素。

Assuming that #dasd# appears only once in the string try this: 假设#dasd#在字符串中仅出现一次,请尝试以下操作:

String a = "asd|asd|#this#|asdasd";


string m = Regex.Match(a, @"#.+#").Value;

Console.WriteLine(m);

.+ searches for any character .+搜索任何字符

IF your string is using the | 如果您的字符串使用| as delimiter, you could also split the string. 作为分隔符,您也可以分割字符串。

string [] array = a.Split('|');

// this would get a list off all values with `#`
List<string> found_all = array.Where(x => x.Contains("#")).ToList();

// this would get the first occurrence of that string. If not found it will return null
string found = array.FirstOrDefault(x => x.Contains("#"))

If your string is a pipe-delimited string, you may split with | 如果您的字符串是用竖线分隔的字符串,则可以使用|分隔| , and grab the value that starts and ends with # . ,并获取以#开头和结尾的值。

var str ="dasdsa|cxzc|12|#dsad#|czxc";
var result = str.Split('|').Where(p => p.StartsWith("#") && p.EndsWith("#")).ToList();
foreach (var s in result)
    Console.WriteLine(s);

See the online C# demo . 请参阅在线C#演示

If you only need one value, use .FirstOrDefault() : 如果只需要一个值,请使用.FirstOrDefault()

var result = str.Split('|')
   .Where(p => p.StartsWith("#") && p.EndsWith("#"))
   .FirstOrDefault();

According to the input given in the Question that you need to search for the string between the two hashtags following will be the regex, 根据问题中给出的输入,您需要搜索下面两个标签之间的字符串将是正则表达式,

^.*#(.*)#.*$

If the data is empty between 2 hashtags, still the regex will not fail. 如果两个标签之间的数据为空,则正则表达式仍不会失败。 It will take empty value. 它将为空值。

This appears to be CSV delimited data with five sections of data. 这似乎是带有五个数据部分的CSV分隔数据。

Extract each of the sections and then project into a dynamic entity using regex to denote each of the sections. 提取每个部分,然后使用正则表达式表示每个部分,将其投影到动态实体中。

string data = "dasdsa|cxzc|12|#dsad#|czxc";

string pattern = @"(?<Section1>[^|]+)\|(?<Section2>[^|]+)\|(?<Section3>[^|]+)\|(?<Section4>[^|]+)\|(?<Section5>[^|]+)";

var results =
Regex.Matches(data, pattern, RegexOptions.ExplicitCapture)
     .OfType<Match>()
     .Select(mt => new
     {
            One = mt.Groups["Section1"].Value,
            Two = mt.Groups["Section2"].Value,
            Three = mt.Groups["Section3"].Value,
            Four = mt.Groups["Section4"].Value,
            Five = mt.Groups["Section5"].Value,
     })
     .ToList();

    Console.WriteLine(results[0].Four ); // #dsad#

Once complete extract from what results contains. 一旦完成,从results包含什么。 For it is just a list of the multi-line captures, where each line contains just is one entity of data for that line. 因为它只是多行捕获的列表,其中每行仅包含该行数据的一个实体。

Extract from the appropriate properties; 从适当的属性中提取; whereas our final result on has one line, but we can get to the data as shown in the example WriteLine : 而我们的最终结果只有一行,但是我们可以如示例WriteLine所示获得数据:

在此处输入图片说明

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

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