2013-12-22 46 views
0

我有點麻煩搞清楚如何匹配這個正則表達式模式。我想要做的是匹配技能名稱,而不是右側的描述。如何使用正則表達式匹配左側的文本?

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

正則表達式無法比擬不規則的文字。如果您使用'\ t'(製表符)分隔符,則可以在製表符之前獲取所有文本。 –

+0

技能和描述之間的特徵是什麼?除非是空間以外的東西,否則看起來你運氣不好。 –

+0

這是所有的空間,沒有選項卡。 –

回答

1

如果您要匹配ALL直到第18個字符,請使用:

^[\w\s]{18} 

然後可以去掉尾隨的空格。

這裏有一個工作示例:http://regex101.com/r/zB1lY7

更簡單的方式來做到這將是split 18日字符,然後取回第一分(group 0),並在該修剪空白。你沒有在你的問題中提供一種語言,所以我不能給你寫一個例子。

+0

我使用Lua,所以'trim()'函數可以完美地工作。謝謝! –

+0

爲什麼使用正則表達式來表示已知邊界的子字符串? –

+0

@maček正好,因此「一個更簡單的方法來做到這一點將是分裂在第18個字符......」 – brandonscript

1

最長的技能,「召回Earthstone」,長17個字符,所以下面的正則表達式匹配的技能,並留下了尾隨空格:

^.{1,16}\w 

這裏有一個演示:http://regex101.com/r/aV1qG1

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

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." 
1
如果要修剪尾隨空格

,您可以在多行模式下使用這個(如果您正則表達式的味道支持lookbehinds):

^.{1,17}(?<!\s) 
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.