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 in a dream 演示视频
Trapped_Heavy
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
for heterolytic cleavage of H2 into protons, bound in surface OH groups, and hydrides, trapped in the vacancies, is low. The stabilized hydride facilitates the reduction of CO to formyl (stabilized in...
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...
陈健荣,陈健武,The intrinsic defects of traditional BPM technologies resulted in many companies’ failure in process management as enterprises trapped in a complex and changeful market environme
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 ...
easily get trapped in a poor local minima, which results in substantial accuracy loss. To mitigate this problem, we propose three simple-yet-effective approaches to improve the network training. First...