【问题标题】:Leading and trailing spaces for each line from textareatextarea 中每一行的前导和尾随空格
【发布时间】:2014-12-09 17:32:43
【问题描述】:
  • 红宝石 2.1.3
  • 导轨 4.1.7

我想从 textarea 生成一个无序列表。所以我必须保留每个项目的所有换行符并删除前导和尾随空格。

好吧,我正在尝试从 textarea 的每一行 中删除所有前导和尾随空格,但没有成功。

我正在使用正则表达式:

string_from_textarea.gsub(/^[ \t]+|[ \t]+$/, '')

我也尝试过 strip 和 rstrip rails 方法,但也没有成功(它们的结果与正则表达式相同):

  • 每行的前导空格已被完美删除。
  • 但对于尾随空格,仅删除字符串中的最后一个空格。但我想为每一行。

我在这里缺少什么?每行的 textarea 和尾随空格是怎么回事?

更新

一些代码示例:

我正在使用回调来保存格式化数据。

after_validation: format_ingredients

def format_ingredients
    self.ingredients = @ingredients.gsub(/^[ \t]+|[ \t]+$/, "")
end

表单视图:

= f.text_area :ingredients, class: 'fieldW-600 striped', rows: '10'

【问题讨论】:

  • 试试/^[^\S\r\n]+|[^\S\r\n]+$/m
  • 最后会不会有一些非ASCII字符?试试这个并报告:line[-5,-1].encode('utf-8').each_char { |c| puts c.ord }.
  • @sin 同样的结果,不幸的是
  • 很奇怪,因为\s 应该涵盖所有 Unicode 中断,而[^\S] 等于\s。您是否在 Unicode 模式下使用正则表达式?也许是//u 或其他东西(不懂Java)。
  • @CarySwoveland 不,我只使用拉丁字母和数字。您的代码为 nil:NilClass 返回了未定义的方法“编码​​”。我已经用更多信息更新了我的问题。谢谢!

标签: ruby-on-rails ruby regex ruby-on-rails-4


【解决方案1】:

您可以使用String#strip

'   test text with    multiple spaces      '.strip
#=> "test text with    multiple spaces"

将此应用于每一行:

str = "   test \ntext with    multiple  \nspaces      "
str = str.lines.map(&:strip).join("\n")
"test\ntext with    multiple\nspaces"

【讨论】:

  • 正如我所写:我尝试了 strip 和 rstrip rails 方法也没有运气(它们的结果与正则表达式相同)
  • 嗯,你试过我的建议了吗?它确实适用于上面的示例。唯一可能出错的是您的字符串以某种方式包含不间断空格。它们看起来像一个空间,但表现得像一个正常的角色。这是常见的错误来源。
  • 对不起,你是对的。你的回答也解决了我的问题。没看到"lines.maplines.map(&:strip).join("\n")",我瞎了。
【解决方案2】:

这不是正则表达式的好用处。而是使用标准的字符串处理方法。

如果您的文本包含嵌入的 LF ("\n") 行尾和行首和行尾的空格,请尝试以下操作:

foo = "
 line 1 
  line 2
line 3
"
foo # => "\n line 1 \n  line 2\nline 3\n"

以下是清除前导/尾随空白行并重新添加行尾的方法:

bar = foo.each_line.map(&:strip).join("\n")
bar # => "\nline 1\nline 2\nline 3"

如果您正在处理 CRLF 行尾,因为 Windows 系统会生成文本:

foo = "\r\n line 1 \r\n  line 2\r\nline 3\r\n"
bar = foo.each_line.map(&:strip).join("\r\n")
bar # => "\r\nline 1\r\nline 2\r\nline 3"

如果您正在处理可能包含其他形式的空白(如不间断空格)的空白,则切换到使用包含空白的 POSIX [[:space:]] 字符集的正则表达式在所有字符集中使用。我会做类似的事情:

s.sub(/^[[:space:]]+/, '').sub(/[[:space:]]+$/, '')

【讨论】:

  • 感谢您的解释和正确的约会!为什么这种方法不适合正则表达式?我认为您的解决方案与 PatrickOscity 给出的解决方案非常相似,但提供了更多信息。
【解决方案3】:

我认为@sin 可能在他/她的第一条评论中暗示了这个问题。您的文件可能是在 Windows 机器上生成的,该机器将回车/生命提要对 ("\r\n") 放在每一行的末尾,而不是(大概)最后一行,它只写 \n。 (检查line[-2] 在除最后一行以外的任何一行。)这将解释您得到的结果:

r = /^[ \t]+|[ \t]+$/

str = "  testing 123  \r\n testing again  \n"
str.gsub(r, '')
  #=> "testing 123  \r\ntesting again\n"

如果这个理论是正确的,那么修复应该只是对你的正则表达式稍作调整:

r = /^[ \t]+|[ \t\r]+$/

str.gsub(r, '')
  #=> "testing 123\ntesting again\n"

您可以通过更改全局变量$/ 的值来使用您的正则表达式执行此操作,该变量是输入记录分隔符,默认为换行符。但是,如果最后一行只有换行符,这可能是最后一行结尾的问题。

【讨论】:

  • 我将 OSX 与 firefox、chrome 和 safari 一起使用。但是你的正则表达式效果很好。感谢您的回答。也适用于“gsub(/^[ \t]+|[ \t]+(.)$/, '').rstrip”。很多人解决了我的问题。我会为我选择最古老和最好的答案。
  • 您是指提交答案的最年长的人,对吧?
【解决方案4】:

我认为您可能正在寻找 String#lstripString#rstrip 方法:

str = %Q^this is a line  
and so is this  
all of the lines have two spaces at the beginning  
and also at the end  ^`

`> new_string = ""
> "" 
str.each_line do |line|
     new_string += line.rstrip.lstrip + "\n"
   end
 > "this is a line\n  and so is this  \n  all of the lines have two spaces at the beginning  \n  and also at the end  " 
2.1.2 :034 > puts new_string
this is a line
and so is this
all of the lines have two spaces at the beginning
and also at the end
> new_string
`> "this is a line\nand so is this\nall of the lines have two spaces at the beginning\nand also at the end\n"`

【讨论】:

  • 我正在从 textarea 生成一个无序列表。所以我必须为每个项目保留所有换行符。正如我所写:我也尝试过 lstrip 和 rstrip rails 方法。
  • 您的示例代码无效。我建议修复它,并准确地复制和粘贴它。另外,请阅读有关格式化代码的帮助,以便您的答案更具可读性。
  • 修复了代码 - 我在循环中调用了错误的字符串变量,但现在可以了。
猜你喜欢
  • 1970-01-01
  • 2011-04-12
  • 2012-05-24
  • 2018-10-04
  • 2013-03-04
  • 1970-01-01
  • 2019-12-16
  • 1970-01-01
  • 2020-08-20
相关资源
最近更新 更多