【发布时间】:2026-01-18 07:00:02
【问题描述】:
我正在尝试将帕斯卡字符串文字输入与以下模式匹配:@"^'([^']|(''))*'$",但这不起作用。模式有什么问题?
public void Run()
{
using(StreamReader reader = new StreamReader(String.Empty))
{
var LineNumber = 0;
var LineContent = String.Empty;
while(null != (LineContent = reader.ReadLine()))
{
LineNumber++;
String[] InputWords = new Regex(@"\(\*(?:\w|\d)*\*\)").Replace(LineContent.TrimStart(' '), @" ").Split(' ');
foreach(String word in InputWords)
{
Scanner.Scan(word);
}
}
}
}
我在输入字符串中搜索任何 pascal-comment 条目,将其替换为空格,然后将输入拆分为子字符串以将它们与以下内容匹配:
private void Initialize()
{
MatchingTable = new Dictionary<TokenUnit.TokenType, Regex>();
MatchingTable[TokenUnit.TokenType.Identifier] = new Regex
(
@"^[_a-zA-Z]\w*$",
RegexOptions.Compiled | RegexOptions.Singleline
);
MatchingTable[TokenUnit.TokenType.NumberLiteral] = new Regex
(
@"(?:^\d+$)|(?:^\d+\.\d*$)|(?:^\d*\.\d+$)",
RegexOptions.Compiled | RegexOptions.Singleline
);
}
// ... Here it all comes together
public TokenUnit Scan(String input)
{
foreach(KeyValuePair<TokenUnit.TokenType, Regex> node in this.MatchingTable)
{
if(node.Value.IsMatch(input))
{
return new TokenUnit
{
Type = node.Key
};
}
}
return new TokenUnit
{
Type = TokenUnit.TokenType.Unsupported
};
}
【问题讨论】:
-
什么是类似 Pascal 的字符串文字? This?
-
你能显示一些输入字符串和预期结果吗?