我試圖將pascal字符串文字輸入匹配到以下模式:@"^'([^']|(''))*'$"
,但這不起作用。模式有什麼問題?類似pascal的字符串文字正則表達式
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);
}
}
}
}
我搜索輸入字符串任何帕斯卡註釋的條目,用空格代替它,然後我分裂輸入成子給他們符合以下幾點:
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?](http://en.wikipedia.org/wiki/String_literal#Double-up_escape_sequence) – 2010-12-20 15:45:25
您能顯示一些輸入字符串和預期結果嗎? – Toto 2010-12-20 15:56:15