Ruby 中未使用的正则表达式捕获

发布于 2024-11-09 11:27:01 字数 472 浏览 0 评论 0原文

我有一个脚本,用于处理 CAD 程序中的文件内容,以便在另一个 CAD 程序中使用。块中未使用的变量是否可以被跳过或写入?该脚本与它们配合得很好,我只是好奇是否有更干净的方法来编写它。谢谢。

    string = IO.read("file.txt")

    string.scan(/regex/m) {|a,b,c,d,e,f,g|

    # captures 7 items, I use 1-4, & 6 below, skipping 5 & 7

    print a, b+".ext", c.to_f/25400000, d.to_f/25400000, f,"\n"
    }

我的问题在于最后一行 - 如果我没有全部使用它们 - 我是否仍然需要声明所有它们,才能使其正常工作并保持正确的顺序?

要素 5 & 7 可能会在以后使用,但现在,它们只是正则表达式的一部分,以方便将来的灵活性。

I have a script that processes the contents of a file from a CAD program, for use in another CAD program. Can the unused variables in the block be skipped, or written around? The script works fine with them in place, I was just curious if there was a cleaner way to write it. Thank you.

    string = IO.read("file.txt")

    string.scan(/regex/m) {|a,b,c,d,e,f,g|

    # captures 7 items, I use 1-4, & 6 below, skipping 5 & 7

    print a, b+".ext", c.to_f/25400000, d.to_f/25400000, f,"\n"
    }

My question lies in the last line - if I'm not using them all - do I still have to declare them all, for it to work properly, and remain in the correct order?

Elements 5 & 7 may be used at a later time, but for now, they are just part of the regex, for future flexibility.

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

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

发布评论

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

评论(2

我很坚强 2024-11-16 11:27:01

由于您将变量作为块变量获取,因此无法跳过该顺序。问题出在你的正则表达式上。如果您不想捕获某个组,则应使用取消捕获组 (?: ) 而不是捕获组 ( )。因此,将正则表达式中的第五个和第七个 ( ) 更改为 (?: )。如果您使用的是 ruby​​ 1.9 或在 ruby​​ 1.8.7 上使用 oniguruma 正则表达式引擎,那么您还可以使用命名捕获;例如,在正则表达式中使用 (?) ,并将块中捕获的字符串引用为 foo$~[:foo]< /代码>。

Since you are getting the variables as block variables, you cannot skip the order. The problem is with your regex. If you have a group that you don't want to capture, you should use the uncapturing group (?: ) instead of the capturing group ( ). So change the fifth and the seventh ( ) in your regex to (?: ). If you are using ruby 1.9 or are using oniguruma regex engine on ruby 1.8.7, then you can also use named captures; for example use (?<foo> ) in the regex, and refer to the captured string in the block as foo or $~[:foo].

巴黎夜雨 2024-11-16 11:27:01

您可以使用数组而不是显式变量列表,然后按索引从数组中挑选内容:

string.scan(/regex/m) { |a|
    print a[0], a[1] + ".ext", a[2].to_f / 25400000, a[3].to_f / 25400000, a[5], "\n"
}

要么修改您的正则表达式以仅捕获您需要的内容。

您可以在列表中多次使用同一个变量,因此只需将不使用的内容重命名为 unused 可能是最简单的选择:

string.scan(/regex/m) { |a, b, c, d, unused, f, unused|
    print a, b + ".ext", c.to_f / 25400000, d.to_f / 25400000, f, "\n"
}

至少这样(或应该)显而易见的是您没有使用第五次和第七次捕获。但是,这在 1.9 中不起作用,因此您必须在 1.9 中使用 unused1unused2

理想的平衡是使用 1.9 的命名捕获组,但 scan 不允许您访问它们。

You could use an array instead of an explicit list of variables and then pick things out of the array by index:

string.scan(/regex/m) { |a|
    print a[0], a[1] + ".ext", a[2].to_f / 25400000, a[3].to_f / 25400000, a[5], "\n"
}

Either that or rework your regular expression to only capture what you need.

You can use the same variable multiple times in the list so just renaming the things you're not using to unused would probably be the simplest choice:

string.scan(/regex/m) { |a, b, c, d, unused, f, unused|
    print a, b + ".ext", c.to_f / 25400000, d.to_f / 25400000, f, "\n"
}

At least this way it is (or should be) obvious that you're not using the fifth and seventh captures. However, this doesn't work in 1.9 so you'd have to use unused1 and unused2 in 1.9.

An ideal balance would be to use 1.9's named capture groups but scan doesn't give you access to them.

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