用于匹配 ARGB 颜色的正则表达式 (-44830298)

发布于 2024-08-06 01:37:52 字数 918 浏览 8 评论 0原文

我正在尝试获取存储在文本文件中的颜色信息,然后使用该颜色作为标签的前色。但是,在运行时,当我单击按钮执行此操作时,它不会给我任何错误消息或任何内容。我的代码如下:

MatchCollection lines = Regex.Matches(File.ReadAllText(Path), @"(.+?)\r\n""([^""]+)""\r\n(\d+),\s(\d+)\r\n([\w\s]*)\r\n(\d+\.\d+)""\r\n(\d+?)");
        foreach (Match match in lines)
            int color = Int32.Parse(match.Groups[5].Value);

我也有其他信息旅馆文件的正则表达式,但其他正则表达式不是问题。问题是我刚刚添加的正则表达式来尝试匹配颜色(argb)。它位于字符串的末尾:

\r\n(\d+?)

据我所知,上面的正则表达式小东西意味着:“找到一个回车符和换行符,然后是 1 个或更多数字,但不要贪婪”。对吗?

有人可以帮我解决这个问题吗?非常感谢。

注意:保存在文本文件中的颜色信息是负数(ARGB 颜色):

-16744193

文件内容如下:

Control
Control Text
Control Location
Control Font Name
Control Font Size
Control Font Color

说明:

Label
"this is a label"
23, 77
Tahoma
14.0
-55794414

因此,正则表达式可以正常工作,它可以正确获取所有其他详细信息,但只是无法获取颜色信息。

i'm trying to get Color information that i've stored in a text file and then use that color as the forecolor for a label. BUT, at run time when i click the button to do it, it doesnt give me any error messages or anything. the code i have is below:

MatchCollection lines = Regex.Matches(File.ReadAllText(Path), @"(.+?)\r\n""([^""]+)""\r\n(\d+),\s(\d+)\r\n([\w\s]*)\r\n(\d+\.\d+)""\r\n(\d+?)");
        foreach (Match match in lines)
            int color = Int32.Parse(match.Groups[5].Value);

I have other regex for other info inn files too, but the other regex isnt the problem. the problem is the regex that i have just added to try and match a color (argb). it's at the end of the string:

\r\n(\d+?)

So as far as I understand it, the above little regex thing means this: "Find a carriagereturn and newline, and then 1 or more digits but dont be greedy". is that right?

can somebody please help me with this. thank you very much.

Note: The color info that is saved in the text file is a negative number (ARGB color):

-16744193

The file contents is like this:

Control
Control Text
Control Location
Control Font Name
Control Font Size
Control Font Color

Explanation:

Label
"this is a label"
23, 77
Tahoma
14.0
-55794414

So, the regex works, it gets all the other details correctly but it just doesnt get the color information.

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

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

发布评论

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

评论(7

影子是时光的心 2024-08-13 01:37:53

我会在出现错误的行上设置一个断点,看看 match.Groups[5].Value 到底是什么。

我打赌它不能转换为 int。

I'd set a breakpoint on the line with the error and see what match.Groups[5].Value really is.

I'm betting it can't be converted to an int.

笑脸一如从前 2024-08-13 01:37:53

按照其他地方的建议打印出匹配值将会有所帮助,并且缩短正则表达式以使其更容易隔离问题。

但我还可以建议一件事。这是从字体大小匹配开始的正则表达式的最后一部分:

..."(\d+\.\d+)\r\n(-?\d+)"

应该匹配

...
14.0
-55794414

由于在字体大小数字匹配之后立即匹配换行符,因此如果 14.0 之后有空格,则匹配将失败。
尝试使用

..."(\d+\.\d+)\s*\r\n(-?\d+)"

which 应该可以在有或没有尾随空格的情况下工作。 (根据您的正则表达式引擎,"\s*" 可能比 "\s*\r\n" 更好。)

实际上还有一大堆其他的可能出错的事情。通常,拆分字符串并使用较小的正则表达式更容易。

我想你已经知道这句话了?

有些人,当遇到
问题,想“我知道,我会用
正则表达式。”现在他们有了
两个问题。

Printing out the match values as suggested elsewhere would be a help, as well as shortening the regexp to make it easier to isolate the problem.

But I can suggest one more thing. Here's the last part of the regexp starting from the font size match:

..."(\d+\.\d+)\r\n(-?\d+)"

Which is supposed to match against

...
14.0
-55794414

Since you have the match for the newline immediately after the match for the font size digits, the match will fail if you have a space after 14.0.
Try with

..."(\d+\.\d+)\s*\r\n(-?\d+)"

which should work both with and without trailing spaces. (Depending on your regexp engine, just "\s*" may be better than "\s*\r\n".)

There are actually a whole bunch of other things that could go wrong. It is generally easier to split up the strings and work with smaller regexps.

I suppose you already know the quote?

Some people, when confronted with a
problem, think "I know, I'll use
regular expressions." Now they have
two problems.

天涯沦落人 2024-08-13 01:37:53

您的正则表达式中只有 4 个组,但您尝试访问第 5 到 8 个组,这些组将是空字符串,并且 Int32.Parse 无法将空字符串解析为整数。

You have only 4 groups in your regular expressions, but you are trying to access groups 5 to 8 which will be empty strings, and an empty string cannot be parsed as an integer by Int32.Parse.

凉城已无爱 2024-08-13 01:37:53

您可以使用这个正则表达式:

@"(.+)\r\n""([^""]+)""\r\n(\d+),\s(\d+)\r\n([\w\s]*)\r\n(\d+\.\d+)\r\n(-?\d+)"

一个使用示例,从中可以看出它的工作原理:

string value = @"Label
""this is a label""
23, 77
Tahoma
14.0
-55794414
Label
""this is a label""
23, 77
Tahoma
14.0
-55794415";

MatchCollection lines = Regex.Matches(
 value,
 @"(.+)\r\n""([^""]+)""\r\n(\d+),\s(\d+)\r\n([\w\s]*)\r\n(\d+\.\d+)\r\n(-?\d+)");
var colors = new List<int>();
foreach (Match match in lines)
{
    colors.Add(Int32.Parse(match.Groups[7].Value));
}

CollectionAssert.AreEquivalent(new[] { -55794414, -55794415}, colors);

在这个示例中,我们有 2 个具有不同颜色的标签,可以看出正则表达式与颜色匹配。

正则表达式的组:

  • 0:控制
  • 1:文本
  • 2:X
  • 3:Y
  • 4 :字体
  • 6:尺寸
  • 7:颜色

You can use this regex:

@"(.+)\r\n""([^""]+)""\r\n(\d+),\s(\d+)\r\n([\w\s]*)\r\n(\d+\.\d+)\r\n(-?\d+)"

A usage example from which it can be seen that it works:

string value = @"Label
""this is a label""
23, 77
Tahoma
14.0
-55794414
Label
""this is a label""
23, 77
Tahoma
14.0
-55794415";

MatchCollection lines = Regex.Matches(
 value,
 @"(.+)\r\n""([^""]+)""\r\n(\d+),\s(\d+)\r\n([\w\s]*)\r\n(\d+\.\d+)\r\n(-?\d+)");
var colors = new List<int>();
foreach (Match match in lines)
{
    colors.Add(Int32.Parse(match.Groups[7].Value));
}

CollectionAssert.AreEquivalent(new[] { -55794414, -55794415}, colors);

In this example we have 2 labels with different colors, as can be seen the regex matches the colors.

The groups of the regex:

  • 0: Control
  • 1: Text
  • 2: X
  • 3: Y
  • 4: Font
  • 6: Size
  • 7: Color
苍暮颜 2024-08-13 01:37:52

-\d 不匹配。另外,如果您对 \d+ 进行非贪婪匹配,则只会捕获第一个数字,因为它满足正则表达式。将您的正则表达式更改为 (-?\d+) 以匹配号码开头的可选 - 以及号码中的尽可能多的数字,最多(但不包括)数字后面的字符(换行符,字符串结尾,...)。

- is not matched by \d. Also, if you do a non-greedy match on \d+ only the first digit will be captured since that satisfies the regexp. Change your regexp into (-?\d+) to match an optional - at the start of your number and as many digits as there are in the number, up to (but not including) the character following the number (newline, end-of-string, ...).

牛↙奶布丁 2024-08-13 01:37:52

尝试这个正则表达式,看看它是否有效。

(.+?)\r\n""([^""]+)""\r\n(\d+),\s(\d+)\r\n([\w\s]*)\ r\n(\d+.\d+)\r\n(-?\d+)

您的正则表达式似乎正在寻找字体大小值后的 "。

此外,您的 ARGB 颜色将在组匹配 7 中,而不是 5。

Try this regex and see if it works.

(.+?)\r\n""([^""]+)""\r\n(\d+),\s(\d+)\r\n([\w\s]*)\r\n(\d+.\d+)\r\n(-?\d+)

Your regex appeared to be looking for the " after the font size value.

Also, your ARGB colour will be in group match 7, not 5.

魔法少女 2024-08-13 01:37:52

您的正则表达式不是问题。你的正则表达式以及 Wim 给出的正则表达式都可以正常工作。所以问题一定出在处理输入的方式上。

您可以尝试以下一些操作:

使用不同的输入运行此命令,即不要使用此数字:

-16744193

使用类似这样的内容:

100
-100

然后打印出此行后的值:

int fcolor = Int32.Parse(match.Groups[7].Value);

看看它是什么。

您应该做的另一件节省时间的事情是打印出其中的值

match.Groups[1].Value
match.Groups[2].Value
match.Groups[3].Value
match.Groups[4].Value
match.Groups[5].Value
match.Groups[6].Value
match.Groups[7].Value
match.Groups[8].Value

并将其发布到此处。这将使您以及每个 SO 人员轻松了解问题所在。

Your regex is not the problem. Your regex as well as the one given by Wim works fine. So the problem has to be with the way the input is being handled.

Here are some things you can try:

Run this with different input, i.e. instead of using this number:

-16744193

Use something like this instead:

100
-100

Then print out the value after this line:

int fcolor = Int32.Parse(match.Groups[7].Value);

And see what it is.

Another time-saving thing you should do is print out the values in

match.Groups[1].Value
match.Groups[2].Value
match.Groups[3].Value
match.Groups[4].Value
match.Groups[5].Value
match.Groups[6].Value
match.Groups[7].Value
match.Groups[8].Value

And post them here. This will make it easy for you as well as every at SO to see what the problem is.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文