[英].net Regex.Split
我正在嘗試將以下字符串"Name=='mynme' && CurrentTime<'2012-04-20 19:45:45'"
拆分為:
Name
==
'myname'
&&
CurrentTime
<
'2012-04-20 19:45:45'
我有以下正則表達式:
([+\\-*/%()]{1}|[=<>!]{1,2}|[&|]{2})
問題是當使用上面的正則表達式我得到以下結果:
Name
==
'myname'
&&
CurrentTime
<
'2012
-
04
-
20
19:45:45'
我幾乎需要正則表達式才能引用。
謝謝
有關lordcheeto答案的更新1:
你的回答很接近。 但以下仍未正確拆分:
string input2 = "((1==2) && 2-1==1) || 3+1==4 && Name=='Stefan+123'";
我需要做的是將字符串拆分為運算符和操作數。 像這樣的東西:
LeftOperand Operator RightOperand
現在,如果任何運算符在''
之間''
則應該忽略它,並且''
之間的整個字符串應該被視為操作數。
上面的字符串應該生成以下輸出:
(
(
1
==
2
)
&&
2
-
1
==
1
)
||
3
+
1
==
4
&&
Name
==
'Stefan+123'
好吧,假設您希望它簡單地拆分邏輯和關系運算符,您可以使用此模式:
string lordcheeto = @"\s*(==|&&|<=|>=|<|>)\s*";
這也將修剪返回字符串中的所有空格。
碼:
using System;
using System.Text.RegularExpressions;
namespace RegEx
{
class Program
{
static void Main(string[] args)
{
string original = "([+\\-*/%()]{1}|[=<>!]{1,2}|[&|]{2})";
string lordcheeto = @"\s*(==|&&|<=|>=|<|>)\s*";
string input = "Name=='mynme' && CurrentTime<45 - 4";
string input1 = "Name=='mynme' && CurrentTime<'2012-04-20 19:45:45'";
string ridiculous = "Name == BLAH && !@#>=$%^&*()< ASDF && this > that";
executePattern("original", input, original);
executePattern("lordcheeto's", input, lordcheeto);
executePattern("original", input1, original);
executePattern("lordcheeto's", input1, lordcheeto);
executePattern("original", ridiculous, original);
executePattern("lordcheeto's", ridiculous, lordcheeto);
}
static void executePattern(string version, string input, string pattern)
{
// Avoiding repitition for this example.
Console.WriteLine("Using {0} pattern:", version);
// Needs to be trimmed.
var result = Regex.Split(input.Trim(), pattern);
// Pipes included to highlight whitespace trimming.
foreach (var m in result)
Console.WriteLine("|{0}|", m);
// Extra space.
Console.WriteLine();
Console.WriteLine();
}
}
}
測試:
輸出:
Using original pattern:
|Name|
|==|
|'mynme' |
|&&|
| CurrentTime|
|<|
|45 |
|-|
| 4|
Using lordcheeto's pattern:
|Name|
|==|
|'mynme'|
|&&|
|CurrentTime|
|<|
|45 - 4|
Using original pattern:
|Name|
|==|
|'mynme' |
|&&|
| CurrentTime|
|<|
|'2012|
|-|
|04|
|-|
|20 19:45:45'|
Using lordcheeto's pattern:
|Name|
|==|
|'mynme'|
|&&|
|CurrentTime|
|<|
|'2012-04-20 19:45:45'|
Using original pattern:
|Name |
|==|
| BLAH |
|&&|
| |
|!|
|@#|
|>=|
|$|
|%|
|^&|
|*|
||
|(|
||
|)|
||
|<|
| ASDF |
|&&|
| this |
|>|
| that|
Using lordcheeto's pattern:
|Name|
|==|
|BLAH|
|&&|
|!@#|
|>=|
|$%^&*()|
|<|
|ASDF|
|&&|
|this|
|>|
|that|
好的,有了額外的限制,你應該可以使用這個:
string lordcheeto = @"\s*('.*?'|&&|==|<=|>=|<|>|\(|\)|\+|-|\|\|)\s*";
這仍將修剪返回字符串中的所有空格。 但是,如果匹配彼此相鄰,它將返回空字符串(例如, Name=='Stefan+123'
)。 這次我無法解決這個問題,但這並不重要。
如果導入System.Linq
和System.Collections.Generic
並使結果成為List<string>
,則可以在一個額外的行中刪除List
中的所有空字符串(這比使用直接for循環慢):
var results = Regex.Split(input.Trim(), pattern).ToList();
results.RemoveAll(x => x == "");
碼:
using System;
using System.Text.RegularExpressions;
namespace RegEx
{
class Program
{
static void Main(string[] args)
{
string lordcheeto = @"\s*('.*?'|&&|==|<=|>=|<|>|\(|\)|\+|-|\|\|)\s*";
string input = "Name=='mynme' && CurrentTime<45 - 4";
string input1 = "Name=='mynme' && CurrentTime<'2012-04-20 19:45:45'";
string input2 = "((1==2) && 2-1==1) || 3+1==4 && Name=='Stefan+123'";
executePattern("lordcheeto's", input, lordcheeto);
executePattern("lordcheeto's", input1, lordcheeto);
executePattern("lordcheeto's", input2, lordcheeto);
Console.ReadLine();
}
static void executePattern(string version, string input, string pattern)
{
// Avoiding repitition for this example.
Console.WriteLine("Using {0} pattern:", version);
// Needs to be trimmed.
var result = Regex.Split(input.Trim(), pattern);
// Pipe included to highlight empty strings.
foreach (var m in result)
Console.WriteLine("|{0}", m);
// Extra space.
Console.WriteLine();
Console.WriteLine();
}
}
}
測試:
輸出:
Using lordcheeto's pattern:
|Name
|==
|
|'mynme'
|
|&&
|CurrentTime
|<
|45
|-
|4
Using lordcheeto's pattern:
|Name
|==
|
|'mynme'
|
|&&
|CurrentTime
|<
|
|'2012-04-20 19:45:45'
|
Using lordcheeto's pattern:
|
|(
|
|(
|1
|==
|2
|)
|
|&&
|2
|-
|1
|==
|1
|)
|
|||
|3
|+
|1
|==
|4
|&&
|Name
|==
|
|'Stefan+123'
|
附加評論:
如果你想要拆分任何其他運算符(例如, <<
, +=
, =
, -=
, >>
)(有很多 ),或者還需要其他任何操作符,請問。
感謝“lordcheeto”的答案,我能夠解決您的解決方案中的類似問題。 我正在分享我的問題和解決方案,以防萬一有類似問題的人。
我必須拆分字符串
"abc < 1 && 124 > 2 || 1243 <= 555";
先入
abc < 1
&&
124 > 2
||
1243 <= 555
我通過使用實現了這一點
string[] condtions = Regex.Split(str, @"\s*('.*?'|&&|\|\|)\s*");
然后我必須將每個條件分開
abc < 1
成
abc
<
1
我通過使用實現了這一點
string[] statements = Regex.Split(condtions[0], @"\s*('.*?'|==|<=|>=|<|>|!=)\s*");
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.