.NET regex 多行帮助 - 正则表达式

发布于 2024-09-29 09:48:01 字数 1040 浏览 6 评论 0原文

我有一个像这样的多行字符串。 Ity 有换行功能。

[站点 Url="http://medportal.domain.edu" Owner="DOMAIN\user1" secondaryOwner="DOMAIN\user2" ContentDatabase="WSS_Content_$1" StorageUsedMB="0.8" StorageWarningMB="0" StorageMaxMB="0" /] [站点 URL =“http://medportal.domain.edu/sites/ahSC”所有者 =“DOMAIN\user1”ContentDatabase =“WSS_Content_ahSC”StorageUsedMB =“22.3”StorageWarningMB =“0”StorageMaxMB =“0”/] [站点 Url="http://medportal.domain.edu/sites/ARCTIC" Owner="DOMAIN\user1" ContentDatabase="WSS_Content_ARCTIC" StorageUsedMB="0.1" StorageWarningMB="0" StorageMaxMB="0" /]

我需要提取并格式化如下所示的字符串:

stsadm.exe -o deletecontentdb -url "http://medportal.domain.edu" -databasename "WSS_Content_$1" -databaseserver myfixedservername

其中两个参数是 Url 和 ContentDatabase。

这种模式几乎可以工作,但它会获取额外的内容,并且无法处理多行

(.)\s(Url=)(?.)\s(.)\s(ContentDatabase=) (?.)\s(StorageUsedMB=)(.*)

替换:

stsadm.exe -o deletecontentdb -url ${url} -databasename ${databasename} -databaseserver myfixedservername

谢谢。

I've got a multiline string like this. Ity has line feeds.

[Site Url="http://medportal.domain.edu" Owner="DOMAIN\user1" SecondaryOwner="DOMAIN\user2" ContentDatabase="WSS_Content_$1" StorageUsedMB="0.8" StorageWarningMB="0" StorageMaxMB="0" /]
[Site Url="http://medportal.domain.edu/sites/ahSC" Owner="DOMAIN\user1" ContentDatabase="WSS_Content_ahSC" StorageUsedMB="22.3" StorageWarningMB="0" StorageMaxMB="0" /]
[Site Url="http://medportal.domain.edu/sites/ARCTIC" Owner="DOMAIN\user1" ContentDatabase="WSS_Content_ARCTIC" StorageUsedMB="0.1" StorageWarningMB="0" StorageMaxMB="0" /]

I need to extract and format out strings that look like this:

stsadm.exe -o deletecontentdb -url "http://medportal.domain.edu" -databasename "WSS_Content_$1" -databaseserver myfixedservername

Where the two arguments are Url and ContentDatabase.

This pattern almost works but it picks up extra stuff and can't handle multiline

(.)\s(Url=)(?.)\s(.)\s(ContentDatabase=)(?.)\s(StorageUsedMB=)(.*)

replace:

stsadm.exe -o deletecontentdb -url ${url} -databasename ${databasename} -databaseserver myfixedservername

Thank you.

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

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

发布评论

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

评论(1

离鸿 2024-10-06 09:48:07

您当前的模式是贪婪的,并且从结果替换来看,匹配程度超出了预期。 “贪婪”意味着 .* 的使用比您预期的要多,因此要使其非贪婪,您需要在其后添加 ? 以便它与可能的最少字符数:.*?

\".*\" 这样的模式是贪婪的,因为您打算让它在遇到的第一个引号处停止,但它实际上会继续匹配内容,直到遇到字符串中的最后一个引号(如果存在) 。非贪婪的解决方案是使用 \".+?\"\"[^\"]+\"

请尝试以下模式:

string pattern = @"\[.+?Url=(?<url>"".+?"").+?ContentDatabase=(?<databasename>"".+?"").+?]";
string replacement = "stsadm.exe -o deletecontentdb -url ${url} -databasename ${databasename} -databaseserver myfixedservername";
string result = Regex.Replace(input, pattern, replacement);
Console.WriteLine(result);

Your current pattern is greedy and matches more than intended judging from the resulting replacement. "Greedy" means the usage of .* is gobbling up more than you intend, so to make it non-greedy you need to add a ? after it so it matches the least amount of characters possible: .*?.

A pattern like \".*\" is greedy because you intend for it to stop at the first quote encountered, but it actually continues matching content till it hits the final quote in the string, if one exists. The non-greedy solution is to use \".+?\" or \"[^\"]+\".

Try this pattern instead:

string pattern = @"\[.+?Url=(?<url>"".+?"").+?ContentDatabase=(?<databasename>"".+?"").+?]";
string replacement = "stsadm.exe -o deletecontentdb -url ${url} -databasename ${databasename} -databaseserver myfixedservername";
string result = Regex.Replace(input, pattern, replacement);
Console.WriteLine(result);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文