正则表达式匹配标签内容,同时省略前导和尾随空格

发布于 2024-09-25 11:31:55 字数 520 浏览 2 评论 0原文

我正在尝试编写一个正则表达式来匹配标签的整个内容,减去任何前导或尾随空格。这是输入的简化示例:

<标签> 文本 >

我只想匹配以下内容(请注意如何修剪匹配前后的空格):

“文本”

我目前正在尝试在 .NET (Powershell) 中使用此正则表达式:

(?<=<tag>(\s)*).*?(?=(\s)*</tag>)

但是,此正则表达式匹配“text”加上标签内的前导空格,这是不希望的。如何修复我的正则表达式以按预期工作?

I am trying to write a regex that matches entire contents of a tag, minus any leading or trailing whitespace. Here is a boiled-down example of the input:

<tag> text </tag>

I want only the following to be matched (note how the whitespace before and after the match has been trimmed):

"text"

I am currently trying to use this regex in .NET (Powershell):

(?<=<tag>(\s)*).*?(?=(\s)*</tag>)

However, this regex matches "text" plus the leading whitespace inside of the tag, which is undesired. How can I fix my regex to work as expected?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

倾城月光淡如水﹏ 2024-10-02 11:31:55

您应该不使用regext来解析html。

请改用解析器。

还:
用于删除正文标签属性的正则表达式 (C#)

另请参阅:正则表达式匹配除 XHTML 自包含标签之外的开放标签

如果所有这些都不能说服您,那么请不要在表达式中间使用点。使用字母数字转义符。你的点正在消耗空白。使用 \w (我认为)代替。

You should not use regext to parse html.

Use a parser instead.

Also:
Regex to remove body tag attributes (C#)

Also also: RegEx match open tags except XHTML self-contained tags

If all that doesn't convince you, then don't use the dot in the middle of your expression. Use the alphanumeric escape. Your dot is consuming whitespace. Use \w (I think) instead.

夏天碎花小短裙 2024-10-02 11:31:55

放弃环顾四周;他们只是让工作变得比需要的更加复杂。相反,请使用捕获组来挑选您想要的部分:

<tag>\s*(.*?)\s*</tag>

您想要的部分可以通过 $matches[1] 形式获得。

Drop the lookarounds; they just make the job more complicated than it needs to be. Instead, use a capturing group to pick out the part you want:

<tag>\s*(.*?)\s*</tag>

The part you want is available as $matches[1].

冷情妓 2024-10-02 11:31:55

使用这些正则表达式去除尾随和前导空格。 /^\s+//\s+$/

Use these regular expressions to strip trailing and leading whitespaces. /^\s+/ and /\s+$/

不弃不离 2024-10-02 11:31:55
        test = "<tag>     test    </tag>";
        string pattern3 = @"<tag>(.*?)</tag>";
        Console.WriteLine("{0}", Regex.Match(test,pattern3).Groups[1].Value.Trim());
        test = "<tag>     test    </tag>";
        string pattern3 = @"<tag>(.*?)</tag>";
        Console.WriteLine("{0}", Regex.Match(test,pattern3).Groups[1].Value.Trim());
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文