2016-01-27 65 views
8

只是爲了得到這一點的方式,我會使用indexsubstr或相似的,因爲他們是我的特殊情況下,顯而易見的解決辦法,但我正在做一個grammar,所以我只能用regex。 :(如何將此Perl5/PCRE轉換爲Perl 6正則表達式?

話雖這麼說,在翻譯Perl5中/ PCRE正則表達式來Perl6正則表達式的建議是很好,所以內容反正,因爲Perl 6的日益普及,它的正則表達式引擎是非常不同的。


Here's a regex to only match a string which doesn't contain any of a given list of characters.
try it here

^(?:(?!\/).)*$ 
^   # assert position at start of string 
(?:   # begin a noncapturing group 
    (?!  # negative lookahead: following regex must not match the string 
     \/  # literal forward slash 
    )  # end negative lookahead 
    .  # any character, once 
)*   # the previous noncapturing group, 0..Inf times 
$   # assert position at end of string 

顯然,並不在Perl 6的許多原因工作。

至於原因如上所述,我想在Perl 6使用這個下面就是我試圖把它翻譯成的基礎上,CTRL-Fnon capturingnegative lookahead荷蘭國際集團the perl6 regex docs

[ \/ <!before .*> \/ <!after .*> || .? ]* 

和故障(我認爲):

[  # begin a noncapturing group which apparently look like a charclass in p6 
\/  # a literal forward slash 
<!before .*> # negative lookahead for the immediately preceding regex (literal /) 
\/  # a literal/
<!after .*> # negative lookbehind for the immediately preceding regex 
|| .? # force this to be a noncapturing group, not a charclass 
]*  # end noncapturing group and allow it to match 0..Inf times 

我實現這個像my regex not-in { ... },然後使用它像/^<not-in>$/。但是,它返回Nil字符串,這意味着它不能正常工作。

我一直沒能找到http://regex101.com相當於對Perl 6,這樣玩弄它並不容易,因爲它是用Perl 5

如何翻譯這Perl 6的?

+0

@ChristopherBottoms我做了,但我不知道什麼相關性對任何事情 – cat

+0

'/^not-in $ /'應該是'/^ $ /'。 – CIAvash

+0

@CIAvash當然它應該!我很累,很抱歉。 – cat

回答

8

簡短的回答

正則表達式匹配只有缺乏前瞻性字符串斜線:/^ <-[/]>* $/

/開始的正則表達式
^的開始字符串

<-[開放負字符類(不-,這將是一個正常字符類)
/字符類將不匹配
]>接近字符類

*零個或多個「拷貝」
這個類的串的 $端的正則表達式

空間在Perl 6正則表達式是默認忽略的
/端。


完整的答案

如果我理解正確的,你只是想匹配不包含斜槓的字符串。在這種情況下,只需使用負面的字符類。

含有ab字符類將被這樣寫入:<[ab]>

含任何除了ab字符類將被這樣寫入:<-[ab]>

含任何除了/字符類將被寫入因此:<-[/]>和用於確保字符串中沒有字符包含正斜槓的正則表達式將是/^ <-[/]>* $/

該代碼當一個字符串缺少一個斜槓,當它包含正斜槓不匹配匹配:

say "Match" if "abc/" ~~ /^ <-[/]>* $/; # Doesn't match 
say "Match" if "abcd" ~~ /^ <-[/]>* $/; # Matches 

爲排除只是檢查的一個字符是首選方式使用index函數。但是,如果您想要排除多個字符,只需使用負字符類以及您不希望在字符串中找到的所有字符。

+0

是的,您對我的問題的解釋是正確的,對不起。我沒有在文檔中看到定義負向視圖的方式,但是謝謝! – cat

7

字面翻譯你原來的正則表達式^(?:(?!\/).)*$到Perl 6的語法是:

^ [ <!before \/> . ]* $ 

這是一個直接翻譯很簡單。

  • 更換(?: ...... )[ ... ]
  • 更換(?! ...... )<!before ... >
  • 默認

其他的一切假設x修改在這個例子中保持不變。

我用一個簡單的測試吧:

say "Match" if "ab/c" ~~ /^ [ <!before \/> . ]* $/; # doesn't match 
say "Match" if "abc" ~~ /^ [ <!before \/> . ]* $/; # Match 
+3

稍微可讀,避免「傾斜牙籤綜合徵」:'^ [。 ] * $'' – mscha

1

只是爲了得到這個出路

你的問題開頭:

只是爲了得到這一點的方式,我會用指標,SUBSTR或類似的,因爲它們是明顯解決方案爲我的具體情況,但我正在寫一個語法,所以我只能使用正則表達式。:(

不是學究氣十足,你可以做到這一點事實上,你可以在Perl的正則表達式中嵌入任意代碼


一個典型的Perl 6的例子:。

/ (\d**1..3) <?{ $/ < 256 }>/# match an octet 

\d**1..3位匹配1至3個十進制數字圍繞該位的(...) parens告訴Perl 6將該匹配存儲在特殊變量$/

<?{ ... }>位是代碼斷言。如果代碼返回true,則正則表達式繼續。如果不是,則回溯或失敗。


使用index等(在這種情況下,我挑選substr-eq)內的正則表達式是煩瑣的,可能精神失常。但它是可行的:

say "a/c" ~~/a <?{ $/.orig.substr-eq: '/', $/.to }> . c /; 
say "abc" ~~/a <?{ $/.orig.substr-eq: '/', $/.to }> . c/

顯示:

「a/c」 
Nil 

(一個匹配對象上調用.orig返回原始的字符串是,或正在對匹配調用.to返回該原始字符串中的索引。這是迄今爲止比賽得到,或已經到目前爲止; "abc" ~~/a { say $/.orig, $/.to } bc /顯示abc1。)

+0

謝謝你教我一些東西!當我在我的通知中看到這個答案時,我期待得到一個像這樣開始我的問題的回顧:) – cat

+1

我不想讓[Audrey](https://en.wikipedia.org/wiki/Audrey_Tang)或者[傑克遜銀河](https://en.wikipedia.org/wiki/Jackson_Galaxy)追捕我。:) – raiph

+0

我在回顧我的一些老問題,我想我從來沒有點擊過你的評論中的鏈接,但這些都很有趣,尤其是巧合,*我是一個跨女性的自由軟件開發者,現在奧黛麗是我的偶像:) – cat

相關問題