简体   繁体   English

正则表达式匹配重复文本除以可以在多行字符串中重复多次的哈希字符

[英]regex to match repetitive text divided by hash character that can repeat multiple times in multi line string

i have the following regex i'm using with C#我有以下正则表达式与 C# 一起使用

^.*(#\w+?#.+?#.+?#.+?#.+?:).*$

I need to be able to extract the last occurrence of a specifically formatted string within a multi line string我需要能够在多行字符串中提取特定格式字符串的最后一次出现

The format can be :格式可以是:

#test12345-abc#slkjs3234-df#slkj23423#slkjdj2343:{asdf9230-232_jk233}

or或者

#test12345-abc#slkjs3234-df#slkj23423#slkjdj2343:

and could have the format appear 1 or more times and back to back see examples below:并且可以让格式出现 1 次或多次并背靠背,请参见下面的示例:

Example 1:示例 1:

jlkjlkjlkjlkj
lkjlkjlj lkjlkj  #test12345-abc#slkjs3234-df#slkj23423#slkjdj2343:{asdf9230-232_jk233}  lkjlj
slkjlj

Example 2:示例 2:

jlkjlkjlkjlkj
lkjlkjlj lkjlkj #test12345-abc#slkjs3234-df#slkj23423#l23lkj22:{asdf9230-232_jk233} #test12345-abc#slkjs3234-df#slkj23423#Other:{asdf9230-232_jk233}  lkjlj
slkjlj

Example 3:示例 3:

jlkjlkjlkjlkj
lkjlkjlj lkjlkj  #test12345-abc#slkjs3234-df#slkj23423#slkjdj2343:  lkjlj
slkjlj

Example 4:示例 4:

jlkjlkjlkjlkj
lkjlkjlj lkjlkj #test12345-abc#slkjs3234-df#slkj23423#l23lkj22: #test12345-abc#slkjs3234-df#slkj23423#Other:  lkjlj
slkjlj

i'm struggling to find the proper regex that would look for both the examples and in the different positions in the examples ??我正在努力寻找合适的正则表达式来查找示例和示例中的不同位置? thanks谢谢

You can use您可以使用

var result = Regex.Match(text, @"(?:#\w+(?:-\w+)?)+(?::{[\w-]+})?", RegexOptions.RightToLeft)?.Value;

See the regex demo .请参阅正则表达式演示 Note that the RegexOptions.RightToLeft option makes the regex engine search for a match from the end of the string, so you get the last match if there are more than one.请注意, RegexOptions.RightToLeft选项使正则表达式引擎从字符串的末尾搜索匹配项,因此如果有多个匹配项,您将获得最后一个匹配项。

Details细节

  • (?:#\\w+(?:-\\w+)?)+ - one or more repetitions of (?:#\\w+(?:-\\w+)?)+ - 一次或多次重复
    • # - a # char # - 一个#字符
    • \\w+ - one or more word chars (letters, digits, underscores) \\w+ - 一个或多个单词字符(字母、数字、下划线)
    • (?:-\\w+)? - an optional non-capturing group matching 1 or 0 occurrences of - and then 1+ word chars - 一个可选的非捕获组匹配 1 或 0 次出现-然后是 1+ 个单词字符
  • (?::{[\\w-]+})? - an optional non-capturing group: - 一个可选的非捕获组:
    • : - a colon : - 一个冒号
    • {[\\w-]+} - a { char, then one or more letters, digits, underscores or hyphens and then a } char. {[\\w-]+} - 一个{字符,然后是一个或多个字母、数字、下划线或连字符,然后是一个}字符。

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

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