简体   繁体   English

RegEx匹配第二行中的数字

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

I need a regex to match a number in the second line. 我需要一个正则表达式来匹配第二行中的数字。 Similar input is like this: 类似的输入是这样的:

^C1.1
xC20 
SS3 
M 4 

Decimal pattern (-?\\d+(\\.\\d+)?) matches all numbers and second number can be get in a loop on the code behind but I need a regular expression to get directly the number in the second line. 十进制模式(-?\\d+(\\.\\d+)?)匹配所有数字,第二个数字可以在后面的代码中循环但我需要一个正则表达式直接获得第二行中的数字。

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

This operates by capturing a single line at the beginning of the input: 这通过在输入的开头捕获一行来操作:

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

Then all the non digit characters, then your numbers. 然后是所有非数字字符,然后是你的数字。

Since you've now repeatedly changed your needs, try this on for size: 由于您现在已经多次改变了您的需求,请尝试使用以下尺寸:

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

我能用这个正则表达式捕获它。

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

Check out group 1 of anything that this matches: 查看匹配的任何内容的第1组:

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

If that doesn't work, try this: 如果这不起作用,试试这个:

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

Both are with multiline NOT set... 两者都是多线未设置...

I would probably use the captured group in /^.*?\\r?\\n.*?(-?\\d+(?:\\.\\d+)?)/ where… 我可能会在/^.*?\\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-
)

If it is a flavor that supports lookbehind then there are other alternatives. 如果它是一种支持lookbehind的味道,那么还有其他选择。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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