0

I have a string "<wpf><xaml><wpf-controls>". I need the string between the tags in array format. How do I get this?

1
  • 1
    You really don't want to parse XML with a regular expression. Use an XML parser like Nokigiri or some specialised library for XAML. But please, don't parse XML with regexes. Commented May 15, 2013 at 10:17

2 Answers 2

2

The regex for this problem is really simple it is: /<(.*?)>/

For the array part is would reference to the answer on how to use one line regular expression to get matched content

EDIT: for array of the insides of the tags use <wpf><xaml><wpf-controls>".scan(/(?:<(.*?)>)*/)

The (?: .. ) groups the tag together and the * says we want 0 or more of that group :)

Sign up to request clarification or add additional context in comments.

4 Comments

"<wpf><xaml><wpf-controls>".scan(/<(.*?)>/) i am getting array of arrays, I need only array of strings
In that case use <wpf><xaml><wpf-controls>".scan(/(?:<(.*?)>)*/) the (?: .. ) groups the tag together and the * says we want 0 or more of that group :)
rubular.com is a nice way to work your regexes out, you can provide a testtring on which you can try your regex while editing it, great help for understanding whats going on
I find this answer pretty hard to read, see my answer for a way to do it without making the regex itself more complex.
0
'<wpf><xaml><wpf-controls>'.scan(/<(.*?)>/).map(&:first)

2 Comments

Can you explain your answer ?
String#scan returns an array of arrays if your regex contains groups. In this case there is exactly one group, so the result array will be [["wpf"],["xaml"],["wpf-controls"]]. So get all the strings from each sub-array via Array#first. You could also use Array#flatten instead, but my solution would also work if there were multiple groups in the regex.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.