Reputation: 1467
If I have:
["eaacbf7e-37b3-509e-b2d1-ddce7f0e1f6e", "f9e52e06-697a-57af-9566-d05fabb001a4",
"19edb822-eccb-5289-8fee-a39cdda66cd5", "83d3ad63-b468-5a1e-ba6c-6b69eb4a3dc5"]
(where the entire thing is a string)
Is there a simple regular expression that I can use to select content within the quotes (quotes included)?
Since the above comes out as a string. I want to use regex to select out each id within the quotes (along with the quotes) and store them into a ruby array.
Upvotes: 1
Views: 79
Reputation: 160551
Why are you getting the string in that format? It looks like JSON output, which, if it is, should be parsed by the JSON module.
require 'json'
require 'pp'
foo = [
"eaacbf7e-37b3-509e-b2d1-ddce7f0e1f6e",
"f9e52e06-697a-57af-9566-d05fabb001a4",
"19edb822-eccb-5289-8fee-a39cdda66cd5",
"83d3ad63-b468-5a1e-ba6c-6b69eb4a3dc5"
]
foo.to_json
=> "[\"eaacbf7e-37b3-509e-b2d1-ddce7f0e1f6e\",\"f9e52e06-697a-57af-9566-d05fabb001a4\",\"19edb822-eccb-5289-8fee-a39cdda66cd5\",\"83d3ad63-b468-5a1e-ba6c-6b69eb4a3dc5\"]"
That's probably the string you're getting. If you parse it using the JSON parser, you'll get back a Ruby array:
pp JSON[ foo.to_json ]
=> ["eaacbf7e-37b3-509e-b2d1-ddce7f0e1f6e",
"f9e52e06-697a-57af-9566-d05fabb001a4",
"19edb822-eccb-5289-8fee-a39cdda66cd5",
"83d3ad63-b468-5a1e-ba6c-6b69eb4a3dc5"]
Upvotes: 1
Reputation: 156414
Try using the String#scan
method with the regular expression /"[^"]+"/
:
ids = str.scan(/"[^"]+"/) # => [ "eaacbf7e-...", "f9e52e06-...", ...]
puts ids
"eaacbf7e-37b3-509e-b2d1-ddce7f0e1f6e"
"f9e52e06-697a-57af-9566-d05fabb001a4"
"19edb822-eccb-5289-8fee-a39cdda66cd5"
"83d3ad63-b468-5a1e-ba6c-6b69eb4a3dc5"
That expression breaks down like so:
str.scan(/"[^"]+"/)
# │├──┘│└─ Another literal quotation mark (").
# ││ └─ Match one or more of the previous thing.
# │└─ A class matching any character except (^) quotation marks.
# └─ A literal quotation mark (").
Upvotes: 2
Reputation: 32787
Simply use this regex
"[^"]*"
[^"]*
says match any character except "
i.e [^"]
0 to many times i.e *
Upvotes: 3