正则表达式:匹配行尾之前的文本
我有一个如下所示的文件:
J6 INT-00113G 227.905 5.994 180 ~!@#$%&^)
J3 INT-00113G 227.905 -203.244 180 12341341312315
U13 EXCLUDES -42.210 181.294 180 QFP128
U3 IC-00276G 5.135 198.644 90 B%GA!@-48
U12 IC-00270G -123.610 -201.594 0 SOP8_000
J1 INT-00112G 269.665 179.894 180 SOIC16_1
J2 INT-00112G 269.665 198.144 180 SOIC16-_2
.. .......... ....... ....... ... ................
我想匹配第 6 列中的最终值,以便将其从列表中删除。第 6 列中的值的长度未确定,可以包含任何字符。所以我想做的是匹配空格之前的最终值。或者只是该行的末尾。
代码:
// Reads the lines in the file to format.
var fileReader = File.OpenText(filePath + "\\Remove Package 1 Endings.txt");
// Creates a list for the lines to be stored in.
var fileList = new List<string>();
// Adds each line in the file to the list.
while (true)
{
var line = fileReader.ReadLine();
if (line == null)
break;
fileList.Add(line);
}
var mainResult = new List<string>();
var theResult = new List<string>();
foreach (var mainLine in fileList)
mainResult.Add(string.Join(" ", mainLine));
foreach (var theLine in mainResult)
{
// PLACEMENT ONE Regex
Match theRegex = Regex.Match(theLine, @"insert the regex here!");
if (theRegex.Success)
theResult.Add(string.Join(" ", theLine));
}
// Removes the matched values from both of the Regex used above.
List<string> userResult = mainResult.Except(theResult).ToList();
// Prints the proper values into the assigned RichTextBoxes.
foreach (var line in userResult)
richTextBox2.AppendText(line + "\n");
我想做的是让文件看起来像这样:
J6 INT-00113G 227.905 5.994 180
J3 INT-00113G 227.905 -203.244 180
U13 EXCLUDES -42.210 181.294 180
U3 IC-00276G 5.135 198.644 90
U12 IC-00270G -123.610 -201.594 0
J1 INT-00112G 269.665 179.894 180
J2 INT-00112G 269.665 198.144 180
问题:
- 任何人都可以帮忙为此想出一个正则表达式吗?
编辑:
添加代码:
var lines = new List<string>(File.ReadAllLines(filePath + "\\Remove Package 1 Endings.txt"));
for (int i = 0; i < lines.Count; i++)
{
var idx = lines[i].LastIndexOf(" ");
if (idx != -1)
lines[i] = lines[i].Remove(idx);
richTextBox1.AppendText(lines[i] + Environment.NewLine
}
I have a file that looks like this:
J6 INT-00113G 227.905 5.994 180 ~!@#$%&^)
J3 INT-00113G 227.905 -203.244 180 12341341312315
U13 EXCLUDES -42.210 181.294 180 QFP128
U3 IC-00276G 5.135 198.644 90 B%GA!@-48
U12 IC-00270G -123.610 -201.594 0 SOP8_000
J1 INT-00112G 269.665 179.894 180 SOIC16_1
J2 INT-00112G 269.665 198.144 180 SOIC16-_2
.. .......... ....... ....... ... ................
And I would like to match the end value in the 6th column in order to remove it from a list. The length of the value in the 6th column is undetermined and can contain any character. So what I would like to do is match the end value before a space. or just the end of the line.
CODE:
// Reads the lines in the file to format.
var fileReader = File.OpenText(filePath + "\\Remove Package 1 Endings.txt");
// Creates a list for the lines to be stored in.
var fileList = new List<string>();
// Adds each line in the file to the list.
while (true)
{
var line = fileReader.ReadLine();
if (line == null)
break;
fileList.Add(line);
}
var mainResult = new List<string>();
var theResult = new List<string>();
foreach (var mainLine in fileList)
mainResult.Add(string.Join(" ", mainLine));
foreach (var theLine in mainResult)
{
// PLACEMENT ONE Regex
Match theRegex = Regex.Match(theLine, @"insert the regex here!");
if (theRegex.Success)
theResult.Add(string.Join(" ", theLine));
}
// Removes the matched values from both of the Regex used above.
List<string> userResult = mainResult.Except(theResult).ToList();
// Prints the proper values into the assigned RichTextBoxes.
foreach (var line in userResult)
richTextBox2.AppendText(line + "\n");
What I am trying to do is get the file to look like this:
J6 INT-00113G 227.905 5.994 180
J3 INT-00113G 227.905 -203.244 180
U13 EXCLUDES -42.210 181.294 180
U3 IC-00276G 5.135 198.644 90
U12 IC-00270G -123.610 -201.594 0
J1 INT-00112G 269.665 179.894 180
J2 INT-00112G 269.665 198.144 180
QUESTION:
- Can anyone help come up with a regex for this?
EDIT:
ADDED CODE:
var lines = new List<string>(File.ReadAllLines(filePath + "\\Remove Package 1 Endings.txt"));
for (int i = 0; i < lines.Count; i++)
{
var idx = lines[i].LastIndexOf(" ");
if (idx != -1)
lines[i] = lines[i].Remove(idx);
richTextBox1.AppendText(lines[i] + Environment.NewLine
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
\S+$
应该可以做到,并启用多行功能。 (不确定如何在... C# 中启用正则表达式标志,是吗?,但在字符串前面添加。(?m)
可以与某些正则表达式引擎配合使用,尽管这不是唯一的方法.)\S
- 匹配任何非空白字符+
- 表示前面的正则表达式元素应该匹配一次或多次$
- 表示匹配字符串末尾,如果启用了多行,则表示匹配行尾。编辑:您正在单独检查每一行,因此无需担心多行内容。
(尽管正如其他人所说,使用正则表达式可能会使事情变得比必要的更加复杂。)
\S+$
should do it, with multiline functionality enabled. (Not sure how exactly you enable regex flags in... C#, is it?, but prepending.(?m)
to the string works with some regex engines, though it's not the only way to do it.)\S
- matches any non-whitespace character+
- indicates that the preceding regex element should be matched one or more times$
- indicates matching to the end of the string, or end of a line if multiline is enabled.EDIT: You're checking each line individually, so no need to worry about multiline stuff.
(Though as stated by others, going with regex for this is probably making things more complicated than necessary.)
我认为你让事情变得比实际情况更复杂;例如,如果按照您的示例进行格式化,则以下内容应该帮助您删除数据的最后一部分,并进行一些调整,例如修剪(显然,错误缓解),我确信这会适合:
请注意可以一下子读取文件的所有行,这并不总是需要的,具体取决于要加载的文件的大小,但我看到您在处理之前加载了每一行 - 在这种情况下我们只能让整个事情变得更加简洁。
I think that you're making this more complex than it really is; for instance, the following should help you removing the last part of the data if formatted as per your example, with a little tweaking, such as trimming (and, obviously, error mitigation), I'm sure this would suit:
Note that it is possible to read all lines of a file in one fell swoop, this isn't always desired depending on the size of the file to be loaded, but I see you're loading each of the lines anyway before processing - in which case we can just make the whole thing more concise.
仅依靠每列都用空格分隔的事实,您可以使用:
Just relying on the fact that each column is separated by spaces you could use: