Ruby on rails textarea中每行的前导空格和尾随空格 ruby 2.1.3 轨道4.1.7
我想从textarea生成一个无序列表。因此,我必须保留每个项目的所有换行符,并删除前导空格和尾随空格 好吧,我正在尝试删除每行textarea中的所有前导空格和尾随空格,但没有成功 我正在使用正则表达式:Ruby on rails textarea中每行的前导空格和尾随空格 ruby 2.1.3 轨道4.1.7,ruby-on-rails,ruby,regex,ruby-on-rails-4,Ruby On Rails,Ruby,Regex,Ruby On Rails 4,我想从textarea生成一个无序列表。因此,我必须保留每个项目的所有换行符,并删除前导空格和尾随空格 好吧,我正在尝试删除每行textarea中的所有前导空格和尾随空格,但没有成功 我正在使用正则表达式: string_from_textarea.gsub(/^[ \t]+|[ \t]+$/, '') 我也尝试过strip和rstrip-rails方法,但没有成功(它们的工作结果与regex相同): 每行的前导空格都被完美删除 但对于尾随空格,仅删除字符串中的最后一个空格。但我想为每一行
string_from_textarea.gsub(/^[ \t]+|[ \t]+$/, '')
我也尝试过strip和rstrip-rails方法,但没有成功(它们的工作结果与regex相同):
- 每行的前导空格都被完美删除
- 但对于尾随空格,仅删除字符串中的最后一个空格。但我想为每一行
after_validation: format_ingredients
def format_ingredients
self.ingredients = @ingredients.gsub(/^[ \t]+|[ \t]+$/, "")
end
表单视图:
= f.text_area :ingredients, class: 'fieldW-600 striped', rows: '10'
您可以使用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"
您可以使用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"
我想您可能正在寻找
String#lstrip
和String#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"`
我想您可能正在寻找
String#lstrip
和String#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"`
我认为@sin可能在他/她的第一次评论中暗示了这个问题。您的文件可能是在Windows计算机上生成的,该计算机在每一行的末尾放置了一个回车/换行符对(
“\r\n”
),而不是(大概)最后一行,它只在最后一行写入\n
。(在除最后一行之外的任何一行上选中行[-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"
您可以通过更改全局变量
$/
的值来使用正则表达式执行此操作,该变量是输入记录分隔符,默认情况下为换行符。然而,如果最后一行的结尾只有一个新行,那么这可能是一个问题。我认为@sin可能在他/她的第一个评论中暗示了这个问题。您的文件可能是在Windows计算机上生成的,该计算机在每一行的末尾放置了一个回车/换行符对(“\r\n”
),而不是(大概)最后一行,它只在最后一行写入\n
。(在除最后一行之外的任何一行上选中行[-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"
您可以通过更改全局变量
$/
的值来使用正则表达式执行此操作,该变量是输入记录分隔符,默认情况下为换行符。但是,如果最后一行的末尾只有一个换行符,那么这可能是一个问题。这不是regexp的好用途。而是使用标准的字符串处理方法
如果文本包含嵌入的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:]
字符集的regexp,该字符集包含所有字符集中使用的空格。我会这样做:
s.sub(/^[[:space:]]+/, '').sub(/[[:space:]]+$/, '')
这不是regexp的好用途。而是使用标准的字符串处理方法 如果文本包含嵌入的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:]
字符集的regexp,该字符集包含所有字符集中使用的空格。我会这样做:
s.sub(/^[[:space:]]+/, '').sub(/[[:space:]]+$/, '')
请尝试
/^[^\S\r\n]+|[^\S\r\n]+$/m
结尾是否有一些非ASCII字符?请尝试此操作并返回报告:行[-5,-1]。编码('utf-8')。每个字符{c |放置c.ord}
@sin相同的结果,不幸的是,字符串范围应包括所有Unicode中断,\s
等于\s
。您是否在Unicode模式下使用正则表达式?可能是一个//u
或什么(不懂Java)。@CarySwoveland不,我只使用拉丁字母和数字。您的代码已为nil:NilClass返回未定义的方法“encode”。我已经用更多的信息更新了我的问题。谢谢请尝试/^[^\S\r\n]+|[^\S\r\n]+$/m
结尾是否有一些非ASCII字符?请尝试此操作并返回报告:行[-5,-1]。编码('utf-8')。每个字符{c |放置c.ord}
@sin相同的结果,不幸的是,字符串范围应包括所有Unicode中断,\s
等于\s
。您是否在Unicode模式下使用正则表达式?可能是一个//u
或什么(不懂Java)。@CarySwoveland不,我只使用拉丁字母和数字。您的代码已为nil:NilClass返回未定义的方法“encode”。我已经用更多的信息更新了我的问题。谢谢正如我所写的:我也尝试过strip和rstrip-rails方法,但运气不佳(它们的工作结果与regex相同),那么,你试过我的建议了吗?对于上面的例子,它确实有效。唯一可能出错的是当字符串中包含不间断空格时。它们看起来像一个空格,但行为却像一个普通角色。这是常见的错误来源。对不起,你是对的。你的回答也解决了我的问题。我没有看到“lines.maplines.map(&:strip.join(“\n”)”,我是盲人。正如我所写的:我已经尝试过了