【问题标题】:Regex to find macro value from a html string正则表达式从 html 字符串中查找宏值
【发布时间】:2015-10-26 06:17:46
【问题描述】:

在我的情况下 使用 c# ,必须找到来自 html(作为字符串)的方括号内的字符串,字符串后面可能有空格(即)[userprincipalname].Also它在刺痛之前可能有空格(即)[ userprincipalname],因为它来自html“&nbsp”是为空格添加的(我使用了trim(),但它不能能够在字符串之前修剪 &nbsp)..因此请帮助我了解 正则表达式以仅在方括号内获得修剪(前后没有空格或 &nbsp)值?

debugged state of macro replacement

【问题讨论】:

  • 何不再做一次替换:attribute.Replace(" ", string.empty);

标签: c# regex macros


【解决方案1】:

请先使用HttpUtility.HtmlDecode(html),再使用Trim

【讨论】:

  • 我可以t use HttpUtility in my c# class as it doesnt 显示任何参考以添加!!,请帮助我如何克服这个问题?
  • 如果您使用的是 .NET Framework 4 Client Profile,请切换到 .NET Framework 4 或 .NET Framework 4.5 等。然后添加对 System.Web 的引用。 dll。
  • 谢谢你#Sky Fang,你的回答解决了我的问题
【解决方案2】:

您可以使用Regex.Replace(source, @"^\s+", "");sourceString.Replace(" ",string.empty)

【讨论】:

  • 我在字符串前有空格为 [" "userprincipalname],请参阅上面“宏替换的调试状态”问题中的图片链接
【解决方案3】:
(?<=[[; ])(\w+)

尝试用这个正则表达式匹配属性,

https://regex101.com/r/nF0rZ5/4

【讨论】:

  • 此正则表达式跳过带有空格或“&nbsp”的宏,而不是检索修剪后的值。注意:我有 =>"[ userprincipalname]" (or) "[" " displayname]" .. 我想要 => "userprincipalname" (or) "displayname"
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-10-31
  • 2012-06-09
  • 1970-01-01
  • 2021-09-15
  • 2012-03-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多