我在这里弄清楚如何匹配这个正则表达式模式时遇到了一些麻烦。我想要做的是匹配技能名称,而不是右边的描述。
Earthstone Creating a physical link to the powers of the earth.
Recall Earthstone Recalling the Earthstone to your hands.
Earthsense Seeing through darkness.
Walkwater Walking on water as if it were stone.
Hand of Eden Channeling powers of the earth.
Dustblast Blasting dust at someone.
Earthquake Creating an earthquake underneath someone.
Stonewall Creating a large wall of stone.
Earth Strength Protective earth magics.
Sandstorm Whipping up a great storm of sand.
Pelting Using the earth to stun your opponent.
Quicksand Causing the earth to take hold of someone.
Stone Encasing someone in stone.
Flesh Releasing someone from stone.
Rockstrike Pelting an enemy with rocks.
Tremor Causing target to lose balance or focus.
Stoneskin Turning your skin to stone.
Gravity Increasing the gravitational pull in an area.
Golem Summoning a stone golem to fight for you.
Pillar of Salt A huge pillar of salt to lift you out of danger.
Landslide Causing a devastating landslide.
Soul of Earth Drawing strength from mountains.
为了进一步说明,每个左侧文本都是技能,右侧的句子是描述。我怎样才能匹配技能?
答案 0 :(得分:1)
最长的技能“Recall Earthstone”长度为17个字符,因此以下正则表达式将匹配技能并省略尾随空格:
^.{1,16}\w
答案 1 :(得分:1)
如果要匹配所有直到第18个字符,请使用:
^[\w\s]{18}
然后您可以删除尾随空格。
以下是一个有效的例子:http://regex101.com/r/zB1lY7
更简单的方法是在第18个字符上split
,然后检索第一个分割(group 0
)并修剪其上的空格。你没有在你的问题中提供一种语言,所以我不能给你写一个例子。
答案 2 :(得分:1)
您可以使用制表符替换这些空格并匹配\t
制表符分隔符,但如果左侧和右侧有其他空格,则无法匹配任意数量的空格。另一个选项是左右之间至少有两个空格,您可以匹配多个空格:\s{2,}
在你的情况下,我建议匹配前18个字符,这似乎是一致的,然后不捕获任何内容:
/(.{18}).*/
然后,您可以使用您正在使用的任何实现语言修剪空白。
以下是演示: http://regex101.com/r/eV3eJ7
然而,不更容易使用正则表达式。不确定您使用什么语言来实现它,因为它没有标记,但这是一个PHP示例:
foreach($lines as $line) {
// output the first 18 characters
// with whitespace trimmed off
echo trim(substr($line, 0, 18)) . PHP_EOL;
}
以下是演示:https://eval.in/81986
答案 3 :(得分:1)
如果您的实施具有正面的背后隐藏,您可以利用输入排列在列中的事实。此正则表达式应将名称保留在第一个捕获组中,将描述保留在第二个捕获组中:
^(.{1,17}?)\s+(?<=^.{18})(.*)$
第二组只会在行开头的18个字符后匹配,而第一组只会匹配到第二组之前到达不间断的空格所需的数量。
我看到你正在使用Lua,所以你需要使用PCRE才能在你的情况下工作。我用lrexlib-pcre尝试过它,效果很好:
local rex = require 'rex_pcre'
local spells = io.open('spells.txt','r')
local rx = rex.new'^(.{1,17}?)\\s+(?<=^.{18})(.*)$'
for line in spells:lines() do
name, description = rx:match(line)
print("spell is \"" .. name .. "\"; description is \"" .. description .. "\"")
end
以上产生了这个输出:
spell is "Earthstone"; description is "Creating a physical link to the powers of the earth."
spell is "Recall Earthstone"; description is "Recalling the Earthstone to your hands."
spell is "Earthsense"; description is "Seeing through darkness."
spell is "Walkwater"; description is "Walking on water as if it were stone."
spell is "Hand of Eden"; description is "Channeling powers of the earth."
spell is "Dustblast"; description is "Blasting dust at someone."
spell is "Earthquake"; description is "Creating an earthquake underneath someone."
spell is "Stonewall"; description is "Creating a large wall of stone."
spell is "Earth Strength"; description is "Protective earth magics."
spell is "Sandstorm"; description is "Whipping up a great storm of sand."
spell is "Pelting"; description is "Using the earth to stun your opponent."
spell is "Quicksand"; description is "Causing the earth to take hold of someone."
spell is "Stone"; description is "Encasing someone in stone."
spell is "Flesh"; description is "Releasing someone from stone."
spell is "Rockstrike"; description is "Pelting an enemy with rocks."
spell is "Tremor"; description is "Causing target to lose balance or focus."
spell is "Stoneskin"; description is "Turning your skin to stone."
spell is "Gravity"; description is "Increasing the gravitational pull in an area."
spell is "Golem"; description is "Summoning a stone golem to fight for you."
spell is "Pillar of Salt"; description is "A huge pillar of salt to lift you out of danger."
spell is "Landslide"; description is "Causing a devastating landslide."
spell is "Soul of Earth"; description is "Drawing strength from mountains."
答案 4 :(得分:1)
如果你想修剪尾随空格,你可以在多线模式下使用它(如果你的正则表达式支持lookbehinds):
^.{1,17}(?<!\s)
答案 5 :(得分:0)
如果您知道最长法术的长度
,则没有理由使用正则表达式local f = io.open("spells.txt", "r")
for line in f:lines() do
name = string.sub(line, 0, 17):gsub("%s+$", "")
desc = string.sub(line, 19)
print(string.format("[%s] %s", name, desc))
end
输出
[Earthstone] Creating a physical link to the powers of the earth.
[Recall Earthstone] Recalling the Earthstone to your hands.
[Earthsense] Seeing through darkness.
[Walkwater] Walking on water as if it were stone.
[Hand of Eden] Channeling powers of the earth.
[Dustblast] Blasting dust at someone.
[Earthquake] Creating an earthquake underneath someone.
[Stonewall] Creating a large wall of stone.
[Earth Strength] Protective earth magics.
[Sandstorm] Whipping up a great storm of sand.
[Pelting] Using the earth to stun your opponent.
[Quicksand] Causing the earth to take hold of someone.
[Stone] Encasing someone in stone.
[Flesh] Releasing someone from stone.
[Rockstrike] Pelting an enemy with rocks.
[Tremor] Causing target to lose balance or focus.
[Stoneskin] Turning your skin to stone.
[Gravity] Increasing the gravitational pull in an area.
[Golem] Summoning a stone golem to fight for you.
[Pillar of Salt] A huge pillar of salt to lift you out of danger.
[Landslide] Causing a devastating landslide.
[Soul of Earth] Drawing strength from mountains.