简体   繁体   English

在Lua的模式匹配中,RegEx相当于“.-”是什么?

[英]What is the RegEx equivalent of “.-” in Lua's pattern matching?

I'm porting some Lua code to JS and I haven't worked with Lua so far. 我正在将一些Lua代码移植到JS,到目前为止我还没有使用过Lua。 There's the Lua pattern "^([^aeiouàèéêíòóôúïü]*)(.-)$" and I found the following explanation for the hyphen here : Lua模式是"^([^aeiouàèéêíòóôúïü]*)(.-)$" ,我在这里找到了连字符的以下解释:

- Match the previous character (or class) zero or more times, as few times as possible. - 尽可能少地匹配前一个字符(或类)零次或多次。

I'm trying to figure out what the equivalent as a regular expression would be. 我想弄清楚正则表达式的等价物是什么。 Also I don't understand why this is needed in the first place - wouldn't ending in (.*)$ suffice? 另外我不明白为什么首先需要这个 - 不会以(.*)$结尾?

In Java, .- is actually equivalent of [\\s\\S]*? 在Java中, .-实际上相当于[\\s\\S]*? or (?s).*? (?s).*? , or - to play it safe - (?s:.*?) , because . ,或 - 为了安全起见 - (?s:.*?) ,因为. in Lua patterns matches any char (including line break chars) and - is the lazy (non-greedy) quantifier that matches 0 or more chars, ie *? 在Lua模式中匹配任何char(包括换行符)和-是匹配0或更多字符的懒惰(非贪婪)量词,即*? in regular NFA regex. 在常规的NFA正则表达式中。

See Lua patterns : Lua模式

. all characters

And then 然后

The `+´ modifier matches one or more characters of the original class. `+´修饰符匹配原始类的一个或多个字符。 It will always get the longest sequence that matches the pattern. 它将始终获得与模式匹配的最长序列。

The modifier `*´ is similar to `+´ , but it also accepts zero occurrences of characters of the class... 修饰符`*´类似于`+´ ,但它也接受类出现的零个字符......

Like `*´ , the modifier `-´ also matches zero or more occurrences of characters of the original class. `*´一样,修饰符`-´也匹配原始类的零个或多个字符。 However, instead of matching the longest sequence, it matches the shortest one. 但是,它不是匹配最长的序列,而是匹配最短的序列。

Actually, that pattern is pretty much equivalend to the corresponding regex in many languages. 实际上,该模式与许多语言中的相应正则表达式几乎相同。 Javascript seems to not have the - quantifier, but you should be able to replace it with .* and it should still work. Javascript似乎没有-量词,但你应该能够用.*替换它,它应该仍然有用。

Try "^([^aeiouàèéêíòóôúïü]*)(.*)$" 试试"^([^aeiouàèéêíòóôúïü]*)(.*)$"

Of course, you can also test this in the Lua REPL: 当然,你也可以在Lua REPL中测试一下:

Lua 5.3.5  Copyright (C) 1994-2018 Lua.org, PUC-Rio
> orig = '^([^aeiou]*)(.-)$'
> modif = '^([^aeiou]*)(.*)$'
> ("jhljkhaaaasjkdf"):match(orig)
jhljkh  aaaasjkdf
> ("jhljkhaaaasjkdf"):match(modif)
jhljkh  aaaasjkdf
> -- QED

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

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