简体   繁体   English

Ruby的波斯语正则表达式

[英]Farsi regular expressions with Ruby

I don't know much about Ruby, but I have this line of code that I would like to know what it exactly does: 我对Ruby的了解不多,但是我想了解一下它的确切功能:

newline.gsub!(/\s+(های)\s+/,'‌\1 ')

I appreciate any help on this. 我对此表示感谢。

The regular expression matches if a string contains the persian phrase with one or more whitespace characters around it (both at the front and back). 如果字符串包含波斯语短语且周围带有一个或多个空格字符(位于正反两面),则正则表达式匹配。

Then it replaces it with the string \\1 . 然后将其替换为字符串\\1 The \\1 refers to the first matched element. \\1表示第一个匹配的元素。 So, it removes all the whitespace around the string and adds one space after the element. 因此,它将删除字符串周围的所有空格,并在元素之后添加一个空格。

Example

I am taking the value test instead of the Parsi phrase, because unicode wasn't working out. 我正在使用价值test而不是Parsi短语,因为Unicode无法正常工作。

newline = "    test   "
=> "    test   "
newline.gsub!(/\s+(test)\s+/,'\1 ') 
=> "test "

The documentation says: 文件说:

gsub!(pattern, replacement) → str or nil

So your expression would return the substituted string if it matched the pattern, else return nil . 因此,如果表达式与模式匹配,则表达式将返回替换的字符串,否则返回nil (Essentially remove all the whitespaces before the farsi string and replace the ones following it with a single whitespace.) (基本上删除波斯语字符串之前的所有空格,并用单个空格替换其后的空格。)

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

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