开发者

Ruby split with regex - regex isn't doing what i want

开发者 https://www.devze.com 2022-12-19 01:36 出处:网络
i have this string string = \"<p>para1</p><p>para2</p><p>para3</p>\"

i have this string

string = "<p>para1</p><p>para2</p><p>para3</p>"

I want to split on the para2 text, so that i get this

["<p>para1</p>", "<p>para3</p>"]

The catch is that sometimes para2 might not be wrapped in p tags (and there might be optional spaces outside the p and inside it). I thought that this would do it:

string.split(/\s*(<p>)?\s*para2\s*(<\/p>)?\s*开发者_运维技巧/)

but, i get this:

["<p>para1</p>", "<p>", "</p>", "<p>para3</p>"]

it's not pulling the start and end p tags into the matching pattern - they should be eliminated as part of the split. Ruby's regular expressions are greedy by default so i thought that they would get pulled in. And, this seems to be confirmed if i do a gsub instead of a split:

string.gsub(/\s*(<p>)?\s*para2\s*(<\/p>)?\s*/, "XXX")
=> "<p>para1</p>XXX<p>para3</p>"

They are being pulled in and got rid of here, but not on the split. Any ideas anyone?

thanks, max


Replace your capturing groups (…) with non-capturing groups (?:…):

/\s*(?:<p>)?\s*para2\s*(?:<\/p>)?\s*/
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号