mikiancom
mikiancom

Reputation: 220

Regexp for matching quoted and unquoted text with attributes

Need little help to form a properly working regexp (in ruby) to match quoted or unquoted strings.

Possibilities that user can give:

filename.png title:Title
"file name with spaces" title:Title alt:"Alternative text"
"filename.png" title:"Title"

Filename part should come out as one group and everything else as another, e.g.

=> 1: filename.png 2: title:Title
=> 1: file name with spaces 2: title:Title alt:"Alternative text"
=> 1: filename.png 2: title:"Title"

Upvotes: 0

Views: 691

Answers (1)

dlras2
dlras2

Reputation: 8486

Does ^(\"[\w\. ]+\"|[\w\.]+)(.*)$ work for you?

If the string starts with a quote, you make sure to find another one, or else you disallow any spaces in the file name.

Note that I only allowed word characters \w, periods, and (optionally) spaces in your file names. You can replaced \w\. with any valid characters (besides quotes) that you want to match.

Upvotes: 2

Related Questions