用于匹配 ARGB 颜色的正则表达式 (-44830298)
我正在尝试获取存储在文本文件中的颜色信息,然后使用该颜色作为标签的前色。但是,在运行时,当我单击按钮执行此操作时,它不会给我任何错误消息或任何内容。我的代码如下:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
我会在出现错误的行上设置一个断点,看看
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.
按照其他地方的建议打印出匹配值将会有所帮助,并且缩短正则表达式以使其更容易隔离问题。
但我还可以建议一件事。这是从字体大小匹配开始的正则表达式的最后一部分:
应该匹配
由于在字体大小数字匹配之后立即匹配换行符,因此如果 14.0 之后有空格,则匹配将失败。
尝试使用
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:
Which is supposed to match against
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
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?
您的正则表达式中只有 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.
您可以使用这个正则表达式:
一个使用示例,从中可以看出它的工作原理:
在这个示例中,我们有 2 个具有不同颜色的标签,可以看出正则表达式与颜色匹配。
正则表达式的组:
You can use this regex:
A usage example from which it can be seen that it works:
In this example we have 2 labels with different colors, as can be seen the regex matches the colors.
The groups of the regex:
-
与\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, ...).尝试这个正则表达式,看看它是否有效。
(.+?)\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.
您的正则表达式不是问题。你的正则表达式以及 Wim 给出的正则表达式都可以正常工作。所以问题一定出在处理输入的方式上。
您可以尝试以下一些操作:
使用不同的输入运行此命令,即不要使用此数字:
使用类似这样的内容:
然后打印出此行后的值:
看看它是什么。
您应该做的另一件节省时间的事情是打印出其中的值
并将其发布到此处。这将使您以及每个 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:
Use something like this instead:
Then print out the value after this line:
And see what it is.
Another time-saving thing you should do is print out the values in
And post them here. This will make it easy for you as well as every at SO to see what the problem is.