Today I was trapped by kind of wierd behavior of Ruby's String#split, here's an example:
def parse_inline_styles(text) segments = text.split(%r{(</?.*?>)}).reject {|x| x.empty?} segments.size == 1 ? segments.first : segments end
This code snippet parse text string by <b>, </b>, <i>,</i>, which is specified by regular expression %r{(</?.*?>)}, the result is an array of parsed string. The caveat is the capturing grouping, if we miss the capture group, the String#split() will behavior differently. Let's see thr RDoc from Ruby core.
split(pattern=$;, [limit]) → anArray
click to toggle source
Divides str into substrings based on a delimiter, returning an array of these substrings.
If pattern is a String, then its contents are used as the delimiter when splitting str. If pattern is a single space, str is split on whitespace, with leading whitespace and runs of contiguous whitespace characters ignored.
If pattern is a Regexp, str is divided where the pattern matches. Whenever the pattern matches a zero-length string, str is split into individual characters. If pattern contains groups, the respective matches will be returned in the array as well.
If pattern is omitted, the value of $; is used. If $; is nil (which is the default), str is split on whitespace as if ` ‘ were specified.
If the limit parameter is omitted, trailing null fields are suppressed. If limit is a positive number, at most that number of fields will be returned (if limit is 1, the entire string is returned as the only entry in an array). If negative, there is no limit to the number of fields returned, and trailing null fields are not suppressed.
When the input str is empty an empty Array is returned as the string is considered to have no fields to split.
It says that: If pattern contains groups, the respective matches will be returned in the array as well. Let's verify it with another simple code snippet:
2.0.0p247 :013 > "a<b>bc".split(/<b>/) => ["a", "bc"] 2.0.0p247 :014 > "a<b>bc".split(/(<b>)/) => ["a", "<b>", "bc"] 2.0.0p247 :015 >
The behavior of String#split is just as the RDoc described, it's kind of wired from a Java developer's eyes, which never include matched result of regex.
相关推荐
本主题围绕“Trapped”这一特定字体展开,通过分析提供的文件名,我们可以推测这是一个包含不同格式和样式的“Trapped”字体系列。 1. **字体样式与设计** "Trapped"字体可能具有独特的设计风格,从文件名如"heavy...
Trapped in a dream 演示视频
"Trapped_Heavy"可能是一个特定字体系列的名字,暗示着一种沉重、封闭或者束缚的艺术风格。这种字体可能被设计用于创建引人注目的标题、海报,或者在需要表达强烈情绪或主题深度的设计中。 1. **字体分类**:首先,...
Revealing the Truth About “Trapped Rainbow” Storage of Terahertz Waves in Plasmonic Grating
This paper presents a practical method for nding the globally optimal solution ... The practical eciency of the algorithm is demonstrated by numerical experiments for synthetic data.
本文讨论了在不同外部势场中被束缚的非广延和广延费米系统的热力学性质。研究基于由Tsallis熵导出的广义统计分布,并详细探讨了系统的总粒子数、总能量和定容热容量等热力学特性。研究发现,这些热力学性质紧密依赖...
082 The Fox Trapped in the Vineyard.doc
This prevents the search from getting trapped in cycles and encourages exploration of new regions in the solution space. - **Aspiration Criteria:** To balance exploration and exploitation, TS ...
例如:“There was a burst of laughter from the next room.”(隔壁房间里突然爆发出一阵笑声。) ### dispose (vi.) 处理、处置的意思,通常与介词“of”连用。例如:“I need to dispose of these old ...
produced when this parameter was a null string (""). Now, the original input filename is used as the AML output filename, with an ".aml" extension. Implemented a generic batch command mode for the ...
在物理学领域,尤其是量子信息处理领域,纠缠态的制备和维持是一个核心议题。文章《受禁原子中的纠缠制备》中,作者郝翔和朱士群探讨了如何通过外部激发原子与受禁原子的相互作用,在受禁原子之间产生纠缠。...
coli at the end of the chain was trapped and oriented via the optical torques yielded by two probes. The orientation of the E. coli was controlled by adjusting the laser power of probe II.
The result is full three-dimensional positional control of trapped particle(s) using a simple and wholly electronic control system. Compared to competing technologies, these devices provide a lower ...
two 3-level atoms are trapped in separate cavities coupled by a short optical fiber, and the atomic entanglement could be realized in a heralded way by detecting the transmission spectrum of the ...
OpenCA: General error trapped 700: The compilation of the command cmdAdvanced_csr failed. Can't use an undefined value as an ARRAY reference at /usr/local/openca- base/lib/openca/functions/mail-utils...
however, PSO is easy to get trapped in local extremum, to have premature convergence or stagnation. In order to help PSO strike a balance between individual diversity and swarm convergence, this ...
we study the optical trapping effects of a diffraction-limited focal field possessing an arbitrary photonic spin and propose a convenient method to manipulate the movement behavior of the trapped ...
The twisted Laguerre-Gaussian beam was generated by transforming of Hermite-Gaussian beams through an optical system consisting of three rotated cylindrical lenses. The intensity distribution and ...
Eigenface method used in face recognition is introduced to reduce the pattern of interference fringes appearing in the absorption image of cold rubidium atom cloud trapped by an atom chip. The ...