我是新的正則表達式。我需要從以下幾行解壓路徑:正則表達式匹配C中的路徑#
XXXX c:\mypath1\test
YYYYYYY c:\this is other path\longer
ZZ c:\mypath3\file.txt
我需要實現返回給定線的路徑的方法。第一列是包含1個或更多字符的單詞,從不爲空,第二列爲路徑。分隔符可以是一個或多個空格,或一個或多個製表符,或兩者兼有。 (這是假設第一列決不會包含空格或製表符)
我是新的正則表達式。我需要從以下幾行解壓路徑:正則表達式匹配C中的路徑#
XXXX c:\mypath1\test
YYYYYYY c:\this is other path\longer
ZZ c:\mypath3\file.txt
我需要實現返回給定線的路徑的方法。第一列是包含1個或更多字符的單詞,從不爲空,第二列爲路徑。分隔符可以是一個或多個空格,或一個或多個製表符,或兩者兼有。 (這是假設第一列決不會包含空格或製表符)
這聽起來像你對我只是想
string[] bits = line.Split(new char[] { '\t', ' ' }, 2,
StringSplitOptions.RemoveEmptyEntries);
// TODO: Check that bits really has two entries
string path = bits[1];
編輯:用正則表達式,你可能只需要做到:
Regex regex = new Regex(@"^[^ \t]+[ \t]+(.*)$");
示例代碼:
using System;
using System.Text.RegularExpressions;
class Program
{
static void Main(string[] args)
{
string[] lines =
{
@"XXXX c:\mypath1\test",
@"YYYYYYY c:\this is other path\longer",
@"ZZ c:\mypath3\file.txt"
};
foreach (string line in lines)
{
Console.WriteLine(ExtractPathFromLine(line));
}
}
static readonly Regex PathRegex = new Regex(@"^[^ \t]+[ \t]+(.*)$");
static string ExtractPathFromLine(string line)
{
Match match = PathRegex.Match(line);
if (!match.Success)
{
throw new ArgumentException("Invalid line");
}
return match.Groups[1].Value;
}
}
路徑可以有空格,所以第二個是非常糟糕的。 – xanatos
@Jon:對不起,我需要一個正規的expresion,因爲我使用的是.NET 1.1,而且我無法訪問StringSplitOptions.RemoveEmptyEntries超載。不管怎麼說,還是要謝謝你! –
@DanielPeñalba:開始時這麼說很有用 - 現在要求.NET 1.1是非常罕見的。將編輯。 –
StringCollection resultList = new StringCollection();
try {
Regex regexObj = new Regex(@"(([a-z]:|\\\\[a-z0-9_.$]+\\[a-z0-9_.$]+)?(\\?(?:[^\\/:*?""<>|\r\n]+\\)+)[^\\/:*?""<>|\r\n]+)");
Match matchResult = regexObj.Match(subjectString);
while (matchResult.Success) {
resultList.Add(matchResult.Groups[1].Value);
matchResult = matchResult.NextMatch();
}
} catch (ArgumentException ex) {
// Syntax error in the regular expression
}
擊穿:
@"
( # Match the regular expression below and capture its match into backreference number 1
( # Match the regular expression below and capture its match into backreference number 2
| # Match either the regular expression below (attempting the next alternative only if this one fails)
[a-z] # Match a single character in the range between 「a」 and 「z」
: # Match the character 「:」 literally
| # Or match regular expression number 2 below (the entire group fails if this one fails to match)
\\ # Match the character 「\」 literally
\\ # Match the character 「\」 literally
[a-z0-9_.$] # Match a single character present in the list below
# A character in the range between 「a」 and 「z」
# A character in the range between 「0」 and 「9」
# One of the characters 「_.$」
+ # Between one and unlimited times, as many times as possible, giving back as needed (greedy)
\\ # Match the character 「\」 literally
[a-z0-9_.$] # Match a single character present in the list below
# A character in the range between 「a」 and 「z」
# A character in the range between 「0」 and 「9」
# One of the characters 「_.$」
+ # Between one and unlimited times, as many times as possible, giving back as needed (greedy)
)? # Between zero and one times, as many times as possible, giving back as needed (greedy)
( # Match the regular expression below and capture its match into backreference number 3
\\ # Match the character 「\」 literally
? # Between zero and one times, as many times as possible, giving back as needed (greedy)
(?: # Match the regular expression below
[^\\/:*?""<>|\r\n] # Match a single character NOT present in the list below
# A \ character
# One of the characters 「/:*?""<>|」
# A carriage return character
# A line feed character
+ # Between one and unlimited times, as many times as possible, giving back as needed (greedy)
\\ # Match the character 「\」 literally
)+ # Between one and unlimited times, as many times as possible, giving back as needed (greedy)
)
[^\\/:*?""<>|\r\n] # Match a single character NOT present in the list below
# A \ character
# One of the characters 「/:*?""<>|」
# A carriage return character
# A line feed character
+ # Between one and unlimited times, as many times as possible, giving back as needed (greedy)
)
"
這看起來非常複雜,基本上在第一組空格/製表符之後獲得所有內容。 –
@JonSkeet我同意。這是一個更一般的Windows路徑正則表達式。 – FailedDev
@FailedDev它不適用於例如「k:\ test \ test」。如果我嘗試像** \\ test \ t><* st **那樣傳遞路徑,它將會有效。我發現這個正則表達式是^(?:[c-zC-Z] \:| \\)(\\ [a-zA-Z _ \ - \ s0-9 \。] +)+'。它根據我的意見正確驗證路徑。找到它[這裏](https://www.codeproject.com/Tips/216238/Regular-Expression-to-Validate-File-Path-and-Exten) – Potato
Regex Tester是一個很好的網站,以測試正則表達式快。
Regex.Matches(input, "([a-zA-Z]*:[\\[a-zA-Z0-9 .]*]*)");
輸入是一個文件還是行個別? –
@RoyiNamir有關係嗎? – username
是的。線和文件的處理是不同的。除非你從tex文件中逐行讀取它,然後你還需要照顧換行符字符等。 –