简体   繁体   中英

Using regex match string that start with dot excluding doubles

I'm looking for a regex to split following string:

.name "Collector Show Stress" .target "target" .curio_result_type "negative" .chance 80% .stress 10.0 .on_hit true .on_miss false.queue true

into: .name "Collector Show Stress" .target "target" .curio_result_type "negative" .chance 80% .stress 10.0 .on_hit true .on_miss false .queue true

I've used following regex to match but it also splits the double: \\.[^.]+

The result I get is: .name "Collector Show Stress" .target "target" .curio_result_type "negative" .chance 80% .stress 10 .0 .on_hit true .on_miss false .queue true

I'm basically a newbie when it comes to regex so any help is appreciated.

I'm using the regex in a C# console app.

Thanks in advance!

To match your example data, you could match the available options for the second part:

\.[^.\s]+ (?:"[^"]+"|true|false|[0-9]+(?:\.[0-9]+)?%?)

Explanation

  • .[^.\\s]+ Match . and 1+ occurrences of any char except a dot or whitespace char
  • (?: Non capture group
    • "[^"]+" Match from the opening till the closing double quote "..."
    • | Or
    • true Match literally
    • | Or
    • false Match literally
    • | Or
    • [0-9]+(?:\\.[0-9]+)? Match 1+ digits with an optional decimal part
    • %? Match an optional percentage sign
  • ) Close group

Regex demo

A less stricter pattern might be to match not a dot, or match a dot when followed by a digit

\.[^.]+ (?:[^.\s]|\.(?=[0-9]))+

Explanation

  • .[^.]+ Match a dot followed by 1+ times any char except a dot
  • (?: Non capture group
    • [^.\\s] Match any char except a dot or whitespace char
    • | Or
    • \\.(?=[0-9]) Match a dot asserting a digit directly to the right
  • )+ Close group and repeat 1+ times

Regex demo

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM