【问题标题】:Regex/String Handling [duplicate]正则表达式/字符串处理 [重复]
【发布时间】:2018-03-29 17:48:05
【问题描述】:

我对 Regex 不太擅长,但是,我相信这是我目前唯一的选择,所以如果有人能够帮助我创建 Regex 模式,我将不胜感激。

基本上,我可以拥有以下一个或多个标签:

[start:**uniqueID**:data1:data2:(..)]**###More Data**[/end:**uniqueID**]

**###More Data** 包含在两个标签中,prepost**start****end** 分别。 开始标签有一个uniqueID,与结束标签相同,用于标识我希望提取的数据的开始和结束。 data1 和 data2 只是占位符,我不知道会写什么,除了 pre 以“[start”开头,post 以“[/end”开头

我的问题是,我想先提取开始标签内的数据(括在 [] 括号内),然后解析 **###More Data**(以 [/end:uniqueID] 结尾)标签。

希望我解释得很好,我将不胜感激。谢谢。

【问题讨论】:

  • 给我们确切的字符串和预期的结果
  • 双星号是您尝试格式化还是字符串的一部分?
  • 你只想要uniqueId
  • 由于论坛编辑器弄乱了我的示例,我已经更改了格式,请告诉我现在是否易于阅读。
  • @Karizan 如果你能回答我的问题,我会帮助你提高可读性。

标签: c# .net regex string


【解决方案1】:

有点像

var matchExpr = @"\[start:([^:]+):[^]]+\](.*)\[/end:\1\]";

分解:

// Match the starting tag
var matchExpr = @"\[start:" + 

// capture the unique id
"([^:]+):" + 

// move over everything until the closing character of the start
// tag, which is the "]" character
"[^]]+\]" + 

// the capture group of interest
"(.*)" + 

// The end tag. Note the back reference to the unique id
// from the start tag.
"\[/end:\1\]";  

try it here

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-27
    • 2018-04-19
    • 2020-10-26
    • 1970-01-01
    相关资源
    最近更新 更多