簡體   English   中英

使用以點開頭的正則表達式匹配字符串不包括雙打

[英]Using regex match string that start with dot excluding doubles

我正在尋找一個正則表達式來拆分以下字符串:

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

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

我使用以下正則表達式進行匹配,但它也將雙倍分割: \\.[^.]+

我得到的結果是: .name "Collector Show Stress" .target "target" .curio_result_type "negative" .chance 80% .stress 10 .0 .on_hit true .on_miss false .queue true

在正則表達式方面,我基本上是一個新手,因此感謝任何幫助。

我在 C# 控制台應用程序中使用正則表達式。

提前致謝!

要匹配您的示例數據,您可以匹配第二部分的可用選項:

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

解釋

  • .[^.\\s]+匹配。 以及 1+ 次出現的任何字符,點或空白字符除外
  • (?:非捕獲組
    • "[^"]+"匹配從開頭到結尾的雙引號"..."
    • | 或者
    • true匹配字面意思
    • | 或者
    • false字面匹配
    • | 或者
    • [0-9]+(?:\\.[0-9]+)? 將 1+ 位數字與可選的小數部分匹配
    • %? 匹配一個可選的百分比符號
  • )關閉群組

正則表達式演示

較不嚴格的模式可能是不匹配一個點,或者匹配一個點后跟一個數字

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

解釋

  • .[^.]+匹配一個點后跟 1+ 次除點以外的任何字符
  • (?:非捕獲組
    • [^.\\s]匹配除點或空格字符以外的任何字符
    • | 或者
    • \\.(?=[0-9])匹配一個點,直接在右邊斷言一個數字
  • )+關閉組並重復 1+ 次

正則表達式演示

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM