Separating out string elements

Hello,
I’ve got a string that’s a series of possible file extensions. It looks
like this:

er*.gif, hlst*.pdf

I want to be able to do a Dir.glob on this string, separating out each
of the two entities. I assume that I want to end up with this.

“er*.gif”, “hlst*.pdf”

So that I can Dir.glob.each on them and do appropriate things with each.

I’ve tried this, with “data” being this string.

data.gsub!(/(^[A-z]+*.[A-z]{3})+,*/, “”\1"")

and I get this.

“” hlst*.pdf

Please help.

Thanks,
Peter

Peter B. wrote in post #1168888:

data.split(/,\s*/).flat_map { |filter| Dir.glob(filter) }

Explication: There are 3 ways to use e regexp :

  • match : check if a string match and extract a
    finite list of subexpression
  • split : extract data separate by e regexp
  • scan : extract all data which match regexp

So, you can also do:
data.scan(/[a-zA-Z0-9_]+.[a-zA-Z0-9_]+/).
flat_map { |filter| Dir.glob(filter) }

Regis d’Aubarede wrote in post #1168889:

Peter B. wrote in post #1168888:

data.split(/,\s*/).flat_map { |filter| Dir.glob(filter) }

Explication: There are 3 ways to use e regexp :

  • match : check if a string match and extract a
    finite list of subexpression
  • split : extract data separate by e regexp
  • scan : extract all data which match regexp

So, you can also do:
data.scan(/[a-zA-Z0-9_]+.[a-zA-Z0-9_]+/).
flat_map { |filter| Dir.glob(filter) }

Thank you very much.
But, unfortunately, when I put either of these statements in, nothing
changes when I run the script.

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