0

I am looking for two Regex (or any better way in Ruby) that will give me specific word out of a string.

The string I have is:

<string name="animal">dog</string>

I want to take out "animal" and "dog" from the string so I need two scripts for each word. I have looked into the available options but could not find a better solution. Any help would be highly appreciated.

Taimoor Hassan
  • 365
  • 2
  • 11
  • 1
    SInce this is XML why not use a XML parser? https://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags – roo Sep 13 '21 at 08:33
  • 2
    A lightweight XML parser for ruby: https://github.com/ohler55/ox – roo Sep 13 '21 at 08:34
  • Note for next time: "Get a word from a string" is really vague question title ... This is XML, so why not put XML in the title? Making us *guess* what data type it really is doesn't help anyone, including yourself. – Tom Lord Sep 13 '21 at 10:39
  • I actually did not want to use XML parser at first just because I can have more command in the file data. But OX parser is doing the trick for me. And yes, I will keep the question title in mind for the next time :D – Taimoor Hassan Sep 15 '21 at 13:54

1 Answers1

3

It looks like XML, so XML parser would be the preferred way of handling it. Which parser to use depends on your specific requirements and environment (maybe you already have nokogiri in your Gemfile?). Here's how to proceed with ox suggested by @roo:

string = '<string name="animal">dog</string>'
parsed = Ox.parse(string)
parsed[:name] #=> "animal"
parsed.text #=> "dog"

If you really want to go with a regex for some reason (not really advisable and really fragile) you can use multiple capturing groups:

regex = /\A<string [^>]*name="([^"]+)"[^>]*>([^<]*)<\/string>\z/
_, name, value = regex.match(str)
name #=> "animal"
value #=> "dog"

Note, that regex is very fragile and will just silently stop working if the xml structure changes.

BroiSatse
  • 44,031
  • 8
  • 61
  • 86
  • Thanks BroiStatse! I actually looked into the other XML parser like nokogiri but wasn't able to get the required result. But OX is actually making it pretty simple and It did the trick for me. – Taimoor Hassan Sep 13 '21 at 09:05
  • @timtim - With Nokogiri, just replace `Ox.parse(string)` with `Nokogiri::XML.parse(string).children.first` and all should work. – BroiSatse Sep 13 '21 at 09:09
  • My workaround is bit different here, I have to make some logic in my code and it doesn't work along with Nokogiri. But with OX, its smooth as butter. Thank again @BroiState – Taimoor Hassan Sep 13 '21 at 09:41