簡體   English   中英

RegEx匹配第二行中的數字

[英]RegEx to match a number in the second line

我需要一個正則表達式來匹配第二行中的數字。 類似的輸入是這樣的:

^C1.1
xC20 
SS3 
M 4 

十進制模式(-?\\d+(\\.\\d+)?)匹配所有數字,第二個數字可以在后面的代碼中循環但我需要一個正則表達式直接獲得第二行中的數字。

/^[^\r\n]*\r?\n\D*?(-?\d+(\.\d+)?)/

這通過在輸入的開頭捕獲一行來操作:

^         Beginning of the string
[^\r\n]*  Anything that isn't a line terminator
\r?\n     A newline, optionally preceded by a carriage return

然后是所有非數字字符,然后是你的數字。

由於您現在已經多次改變了您的需求,請嘗試使用以下尺寸:

/(?<=\n\D*)-?\d+(\.\d+)?/

我能用這個正則表達式捕獲它。

.*\n\D*(\d*).*\n

查看匹配的任何內容的第1組:

^.*?\r\n.*?(\d+)

如果這不起作用,試試這個:

^.*?\r\n.*?(\d+)

兩者都是多線未設置...

我可能會在/^.*?\\r?\\n.*?(-?\\d+(?:\\.\\d+)?)/ ? /^.*?\\r?\\n.*?(-?\\d+(?:\\.\\d+)?)/ ? /^.*?\\r?\\n.*?(-?\\d+(?:\\.\\d+)?)/ .*?(-? /^.*?\\r?\\n.*?(-?\\d+(?:\\.\\d+)?)//^.*?\\r?\\n.*?(-?\\d+(?:\\.\\d+)?)/ ?)/中使用捕獲的組...

^                  # beginning of string
.*?                # anything...
\r?\n              # followed by a new line
.*?                # anything...
(                  # followed by...
   -?              # an optional negative sign (minus)
   \d+             # a number
   (?:             #   -this part not captured explicitly-
       \.\d+       # a dot and a number
   )?              #   -and is optional-
)

如果它是一種支持lookbehind的味道,那么還有其他選擇。

暫無
暫無

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

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