Bertaud
Bertaud

Reputation: 2928

regex expression with optional parts

I want to create a regular expression for this text:

 12.34  , 56.78  , "string one"  , "string two"

Here is my regex:

\s+(\d+).(\d+)\s+,\s+(\d+).(\d+)\s+,\s+(".*?")\s+,\s+(".*?")

Now I want to complicate the decoding: the strings can be present or not.
Examples:

12.34,56.78
12.34,56.78,"string one"
12.34,56.78,"","string two"

How can I modify my regex ?

Upvotes: 0

Views: 83

Answers (2)

Shlomo
Shlomo

Reputation: 14370

Use something like this:

(\s*(\d+\.\d+|"[^"]*")\s*,\s*)*(\s*(\d+\.\d+|"[^"]*")\s*)
  • The first part (\s*(\d+\.\d+|"[^"]*")\s*,\s*)* is for matching terms which end with a comma.
  • The last part (\s*(\d+\.\d+|"[^"]*")\s*) matches the last element (no comma).
  • The | in the middle is alteration, meaning match either the digit format, or the string format.
  • "[^"]*" means first match a quote, then match any number of characters that isn't a quote, then match another quote (your string format).

See https://regex101.com/r/jS4sX9/1

Upvotes: 0

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 627468

You can use optional non-capturing groups to make parts of your pattern optional:

Here is the updated regex:

\s*(\d+)\.(\d+)\s*,\s*(\d+)\.(\d+)\s*(?:,\s*(".*?"))?(?:\s*,\s*(".*?"))?
                                     ^^^           ^^^^^               ^  

See demo

Upvotes: 1

Related Questions