简体   繁体   English

是否可以在单词列表标记中转义空格?

[英]Is it possible to escape a space in a word list sigil?

I'm trying to create an atom list with all the provinces of Argentina. 我正在尝试与阿根廷所有省份创建原子列表。 Since some of the are composed of multiple words, I need to escape the whitespace. 由于其中一些是由多个单词组成的,因此我需要转义空白。

~w[
  Buenos\sAires
  Capital\sfederal Catamarca Chaco Chubut Corrientes Córdoba
  Entre\sRíos Formosa Jujuy La\sPampa La\sRioja Mendoza Misiones Neuquén
  Río\sNegro Salta San\sJuan San\ Luis Santa\sCruz Santa\sFe
  Santiago\sdel\sEstero Tierra\sdel\sFuego Tucumán
]a

I've tried using sigil_w and sigil_W , with the classic escaping sequences \\ and \\s , but it does not work. 我尝试使用sigil_wsigil_W ,并使用经典的转义序列\\\\s ,但是它不起作用。

(26 provinces - 1) * (2 quotes (") + 1 comma (,) + 1 space for readability(" ")) - 25 spaces I would use with the sigil form = 75 characters I could save (26个省-1)*(2个引号(“)+ 1个逗号(,)+ 1个可读性空格(”“)))-我将使用sigil格式使用25个空格= 75个字​​符,我可以保存

I know I can achieve this using a custom sigil: 我知道我可以使用自定义标记来实现:

defmodule MySigil do
  def sigil_X(string, [])  do
   string
   |> String.split(~r/\s/)
   |> Enum.map(&String.replace(&1, "\\s", " "))
  end
end

import MySigil
~X[Buenos\sAires La\sPlata]
["Buenos Aires", "La Plata"]

... but is there less "homemade" approach? ...但是“自制”方法较少吗?

No, this is not possible. 不,这是不可能的。 sigil_w and sigil_W both call the private split_words method in the Kernel module, which uses String.split to split the string which will always split on any whitespace. sigil_wsigil_W调用了 Kernel模块中的private split_words方法,该方法使用String.split拆分将始终在任何split_words拆分的字符串。

While it's not possible without hacks, there is a kludge around. 尽管没有黑客手段是不可能的,但周围还是有矛盾的。 Copy-paste the code below into your iex and execute it: 将以下代码复制粘贴到您的iex并执行:

~w|a b c|
#⇒ ["a b", "c"]

[ NB unfortunately, SO has the original symbol converted to normal space, hence copy-paste won't work out of the box] I can hear your “wut?” :) [ 注意,不幸的是,SO会将原始符号转换为普通空间,因此复制粘贴将无法立即使用] 我可以听到您的“哇?” :)

This is achieved with a non-breakable space . 这是通过不易碎的空间实现的 If you are fine with this, just use nbsp and you are all set. 如果您对此感到满意,只需使用nbsp即可。

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

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