簡體   English   中英

如何使用樹頂解析包含空格和轉義符號的解析目錄路徑?

[英]How to parse parse directory path containing whitespaces and escaped symbols using treetop?

我需要解析一些包含目錄路徑的字符串。 問題是包含轉義的空格和其他轉義的符號。 例如:

"/dir_1/dir_2/dir_3/dir/another/dest_dir\ P\&G/"

請注意,在P\&G/之前有一個空格。

這是我的樹頂語法(alpha_digit_special 開頭包含空格)

rule alpha_digit_special
  [ a-zA-Z0-9.+&\\]
end

rule path_without_quotes
  ([/] alpha_digit_special*)+ 
end

rule quot_mark
  ["]
end

rule path_with_quotes
  quot_mark path_without_quotes quot_mark
end

rule path
  path_with_quotes / path_without_quotes
end

解析此字符串后我得到nil 那么我如何指定規則以使字符串可能包含轉義的空格?

您不能使用 alpha_digit_special* 來處理反斜杠轉義空格。 相反,您必須使用字符單元的重復,其中字符單元是反斜杠字符對或單個非反斜杠字符。 這樣的事情應該有效:

rule alpha_digit_special
  [a-zA-Z0-9.+&\\]
end

rule path_character
  '\\' (alpha_digit_special / ' ')
  /
  alpha_digit_special
end

rule path_without_quotes
  ([/] path_character* )+ 
end

請注意,上面的內容不接受反斜杠字符(這不是空格,也不在 alpha_digit_special 集合中)。 我想你可以看到如何改變它。

你試過\s了嗎?

test = "dest_dir P&G" 
test.match(/[a-zA-Z0-9_\s\&]+/)
 => #<MatchData "dest_dir P&G">

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM