String scan or regex to extract word between patterns

Hello -

The following text is stored in a String variable called a:

“\n\n\n\nID=1\n\nName=Application
Server\n\nParentID=0\n\n\n\n\n\nID=36\n\nName=.NET Framework 3.5.1
Features\n\nParentID=0\n\n\n\n\n\nID=41\n\nName=Windows
Process Activation
Service\n\nParentID=0\n\n\n\n\n\nID=49\n\nName=Message Queuing”

When I run the following:

a.scan /Name=.*/

I get the following array:

[“Name=Application Server”, “Name=.NET Framework 3.5.1 Features”,
“Name=Windows Process Activation Service”, “Name=Message Queuing”]

Question: How would I return the above array WITHOUT the String “Name=”
prepended to the values?

i.e. Here is the result I am hoping to get:
[“Application Server”, “.NET Framework 3.5.1 Features”, “Windows Process
Activation Service”, “Message Queuing”]

Thank you!

You can use the zero-width positive lookbehind:

a.scan /(?<=Name=).*/

Or, you can simply do this:

results = []

str.scan(/Name=(.*)/) do |(group1)|
results << group1
end

p results

–output:–
[“ApplicationServer”, “.NET Framework 3.5.1 Features”, “Windows Process
ActivationService”, “Message Queuing”]

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs