简体   繁体   English

正则表达式匹配一条线的2个不同部分

[英]Regex to match 2 different parts of a line

I'm working in lua, and i need to match 2 parts of a line that's taken in through file IO. 我正在lua工作,我需要匹配通过文件IO接收的一行的2部分。 I'm inexperienced with regexes and i'm told lua doesn't have full regex support built in (but i have a library that provides that if need be). 我对正则表达式没有经验,我被告知lua并未内置完整的正则表达式支持(但是我有一个库,如果需要的话可以提供)。 Can someone help me with building regexes to match the parts necessary? 有人可以帮助我构建正则表达式以匹配必要的部分吗?

    "bor_adaptor_00.odf" 3.778
         ^^^^^^^^^^^^^^      ^^^^^
         i need this in      and this in
         a string            a number

I made an example: 我举了一个例子:

s = '"bor_adaptor_00.odf" 3.778'
val1, val2 = string.match(s,'(%b"")%s*([.0-9]*)')
print(val1, val2)

output: 输出:

"bor_adaptor_00.odf"    3.778
^"(.*?)"\s+(\d[\d.]*)$

Explanation: 说明:

  • ^ = line start ^ =行首
  • "(.*?)" = save everything between " and " to a capture group “(。*?)” =将“和”之间的所有内容保存到捕获组
  • \\s+ = any number >= 1 of whitespace chars \\ s + =任何大于等于1的空白字符
  • (\\d[\\d.]*) = a digit followed by more digits or dots (\\ d [\\ d。] *)=一个数字,后接更多数字或点
  • $ = end of line $ =行尾

No idea how to use that in lua, but should help to get you started. 不知道如何在lua中使用它,但是应该可以帮助您入门。

On the other hand, this is a really simple string, so it could be a good idea to parse it without regular expressions. 另一方面,这是一个非常简单的字符串,因此在不使用正则表达式的情况下进行解析可能是一个好主意。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM