引用我的报价!


13

报价是“一种表达的重复作为另一个的一部分”。在美式英语中,引号与双引号(“)一样,如上一句话。但是,当引号出现在另一个引号内时,此规则有所不同。在这种情况下,内部引号由单引号引起来(')。

挑战

给定一个字符串作为输入,请通过交换用于内部引号的双引号和单引号来输出该字符串的引用版本。

不过,只有一个问题-并非所有的单引号都用于引号!对于此挑战,当且仅当以下情况下,才使用单引号引起来:

  • 标记之前是非字母数字字符,或者
  • 标记后跟非字母数字字符,没有空格

报价中使用的标记必须保证平衡;也就是说,您不需要处理无效的输入。此外,双引号将仅在单引号可用于表示引号的情况下使用。

测试用例

A man, a plan, a canal, Panama.
"A man, a plan, a canal, Panama."

Socrates once said, "An unexamined life is not worth living."
"Socrates once said, 'An unexamined life is not worth living.'"

@musicman523 said, "Socrates once said, 'An unexamined life is not worth living.'"
"@musicman523 said, 'Socrates once said, "An unexamined life is not worth living."'"

"Nested 'quotes "can 'cause "recursion," if,' it," runs,' deep!"
"'Nested "quotes 'can "cause 'recursion,' if," it,' runs," deep!'"

"Contractions' behaviors can't be easy to account for," he warned.
"'Contractions' behaviors can't be easy to account for,' he warned."

@carusocomputing said, "Nested, 'quotes, it's unfortunate', to be sure."
"@carusocomputing said, 'Nested, "quotes, it's unfortunate", to be sure.'"

计分

这是,因此每种语言中最短的答案将获胜!



2
... if' it" runs' deep"的单引号前面没有非字母数字字符,也没有跟着“不是空格的非字母数字字符”,但是它们正在转换!
Value Ink 2015年

1
就我个人而言,我认为处理撇号不会给挑战带来很多价值。
ATaco

1
@JonathanAllan是的。我认为,在不使规则复杂化的情况下,很难确定是将单引号引起来还是将其引起使用。我认为这本身将构成一个体面的挑战。
musicman523

1
"Nested, 'quotes, it's unfortunate', to be sure."-我认为您需要一个带有收缩的套盒。
Magic Octopus Urn

Answers:



2

视网膜34 27字节

-7个字节,感谢@Leo

T`'"`"'`\W'|"|'[^\w ]
^|$
"

在线尝试!


通过@carusocomputing said, "Nested, 'quotes, it's unfortunate', to be sure."输入,单引号unfortunate不会更改为双引号,而应该更改为双引号。如果它可以帮助您调试,则当逗号在引号之前时可以使用。(我不知道Retina,所以我真的无法进一步帮助。)
musicman523

@ musicman523解决了它
ovs

您可以直接在角色类中使用\ w
Leo

0

JavaScript(ES6),90个字节

假设字符串不包含#

s=>'"'+s[r='replace'](/"/g,'#')[r](/(\W)'/g,'$1"')[r](/'([^\w ])/g,'"$1')[r](/#/g,"'")+'"'

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.