我正在尋找與以下內容匹配的 .NET Regex模式:
- 字串以
[字符開頭 - 后跟一個整數或十進制數
- 后跟
..(空格字符、點、點、空格字符) - 后跟一個整數或十進制數
- 后跟字串的最后一個字符,即
)
*- 十進制數字有一個小數分隔符,.字符
*- 整數或十進制數的整數值最多為 4 位
*- 十進制數最多應有 4 個小數位
*- 數字可以是負數
*- 如果數字是正數,則 符號缺失
*- 這兩個數字中的哪一個更小無關緊要("[56 .. 55)"例如,第一個數字可以大于第二個數字)
模式應與以下內容匹配:
"[10 .. 15)"
"[100 .. 15.2)"
"[10.431 .. 15)"
"[-10.3 .. -5)"
"[-10.4 .. 5.12)"
"[10.4312 .. -5.1232)"
如果模式匹配,我還想從字串中獲取 2 個數字作為字串:
獲取"10"和"15"從"[10 .. 15)"
獲取"-10.4"和"5.12"從"[-10.4 .. 5.12)"
uj5u.com熱心網友回復:
您可以使用
^\[(-?\d{1,4}(?:\.\d{1,4})?) \.\. (-?\d{1,4}(?:\.\d{1,4})?)\)$
請參閱正則運算式演示。詳情:
^- 字串的開始\[- 一個[字符(-?\d{1,4}(?:\.\d{1,4})?)- 第 1 組:一個可選-的 1 到 4 位數字,然后是 a.和 1 到 4 位數字的可選序列\.\.- 一個..字串(-?\d{1,4}(?:\.\d{1,4})?)- 第 2 組:一個可選-的 1 到 4 位數字,然后是 a.和 1 到 4 位數字的可選序列\)- 一個)字符$- 字串結尾(\z如果您需要檢查字串的結尾,請使用)。
請參閱C# 演示:
var texts = new List<string> { "[10 .. 15)", "[100 .. 15.2)", "[10.431 .. 15)", "[-10.3 .. -5)", "[-10.4 .. 5.12)", "[10.4312 .. -5.1232)", "[12345.1234 .. 0)", "[1.23456 .. 0" };
var pattern = new Regex(@"^\[(-?\d{1,4}(?:\.\d{1,4})?) \.\. (-?\d{1,4}(?:\.\d{1,4})?)\)$");
foreach (var s in texts)
{
Console.WriteLine($"---- {s} ----");
var match = pattern.Match(s);
if (match.Success)
{
Console.WriteLine($"Group 1: {match.Groups[1].Value}, Group 2: {match.Groups[2].Value}");
}
else
{
Console.WriteLine($"No match found in '{s}'.");
}
}
輸出:
---- [10 .. 15) ----
Group 1: 10, Group 2: 15
---- [100 .. 15.2) ----
Group 1: 100, Group 2: 15.2
---- [10.431 .. 15) ----
Group 1: 10.431, Group 2: 15
---- [-10.3 .. -5) ----
Group 1: -10.3, Group 2: -5
---- [-10.4 .. 5.12) ----
Group 1: -10.4, Group 2: 5.12
---- [10.4312 .. -5.1232) ----
Group 1: 10.4312, Group 2: -5.1232
---- [12345.1234 .. 0) ----
No match found in '[12345.1234 .. 0)'.
---- [1.23456 .. 0 ----
No match found in '[1.23456 .. 0'.
uj5u.com熱心網友回復:
以下正則運算式應該沒問題。
^\[-?\d (?:\.\d )? \.\. -?\d (?:\.\d )?\)$
var pattern = @"^\[-?\d (?:\.\d )? \.\. -?\d (?:\.\d )?\)$";
var inputs = new[]{"[10 .. 15)", "[100 .. 15.2)", "[10.431 .. 15)", "[-10.3 .. -5)", "[-10.4 .. 5.12)", "[10.4312 .. -5.1232)", };
foreach (var input in inputs)
{
Console.WriteLine(input " = " Regex.IsMatch(input, pattern));
}
// [10 .. 15) = True
// [100 .. 15.2) = True
// [10.431 .. 15) = True
// [-10.3 .. -5) = True
// [-10.4 .. 5.12) = True
// [10.4312 .. -5.1232) = True
https://dotnetfiddle.net/LpswtI
uj5u.com熱心網友回復:
這有效(見這個 .Net Fiddle:
using System;
using System.Text.RegularExpressions;
public class Program
{
public static void Main()
{
Match m = rx.Match("[123 .. -9876.5432]");
if (!m.Success )
{
Console.WriteLine("No Match");
}
else
{
Console.WriteLine(@"left: {0}", m.Groups[ "left" ] );
Console.WriteLine(@"right: {0}", m.Groups[ "right" ] );
}
}
private static readonly Regex rx = new Regex(@"
^ # anchor match at start-of-text
[[] # a left square bracket followed by
(?<left> # a named capturing group, containing a number, consisting of
-?[0-9]{1,4} # - a mandatory integer portion followed by
([.][0-9]{1,4})? # - an optional fractional portion
) # the whole of which is followed by
[ ][.][.][ ] # a separator (' .. '), followed by
(?<right> # another named capturing group containing a number, consisting of
-?[0-9]{1,4} # - a mandatory integer portion followed by
([.][0-9]{1,4})? # - an optional fractional portion
) # the whole of which is followed by
\] # a right square bracket, followed by
$ # end-of-text
",
RegexOptions.IgnorePatternWhitespace|RegexOptions.ExplicitCapture
);
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/411029.html
標籤:
