私はC#でKB/AJAX /とAjaxのRegexクラスと/C#での正規表現で有用なデータを抽出する方法を
<a href="/KB/ajax/" id="ctl00_MC_TCRp_ctl01_TSRp_ctl01_TSNL">Ajax</a>
が、今私が取得したい文字列を持っています。
誰でも手伝ってもらえますか?
どうもありがとう
乾杯
私はC#でKB/AJAX /とAjaxのRegexクラスと/C#での正規表現で有用なデータを抽出する方法を
<a href="/KB/ajax/" id="ctl00_MC_TCRp_ctl01_TSRp_ctl01_TSNL">Ajax</a>
が、今私が取得したい文字列を持っています。
誰でも手伝ってもらえますか?
どうもありがとう
乾杯
string url = "<a href=\"/KB/ajax/\" id=\"ctl00_MC_TCRp_ctl01_TSRp_ctl01_TSNL\">Ajax</a>";
Regex finder = new Regex("href=\"([^\"]*)\"");
string first = finder.Match(url).Groups[1].Value;
finder = new Regex(">([^<]*)<");
string second = finder.Match(url).Groups[1].Value;
次の正規表現も<a>...</a>
で囲まれたテキスト内のタグが可能になります:
<\s*a\b[^>] href\s*=\s*['"]([^"']*)['"][^>]*>((?:.(?!</a))*.)</a
C#.NETのコード例:
using System;
using System.Text.RegularExpressions;
namespace myapp
{
class Class1
{
static void Main(string[] args)
{
String sourcestring = "source string to match with pattern";
Regex re = new Regex(@"\<\s*a\b[^\>]+href\s*=\s*['""]([^""']*)['""][^\>]*\>((?:.(?!<\/a))*.)\<\/a");
MatchCollection mc = re.Matches(sourcestring);
int mIdx=0;
foreach (Match m in mc)
{
for (int gIdx = 0; gIdx < m.Groups.Count; gIdx++)
{
Console.WriteLine("[{0}][{1}] = {2}", mIdx, re.GetGroupNames()[gIdx], m.Groups[gIdx].Value);
}
mIdx++;
}
}
}
}
(
[0] => Array
(
[0] => <a href="/KB/ajax/" id="ctl00_MC_TCRp_ctl01_TSRp_ctl01_TSNL">Aj<b>a</b>x</a
)
[1] => Array
(
[0] => /KB/ajax/
)
[2] => Array
(
[0] => Aj<b>a</b>x
)
)
非常に強力なregのexp、私はMatchCollectionを使用する方法を学んだ、ありがとうダニエル:) – Franva
文字列と何があなたの代わりに、このためのXMLパーサーを使用して検討するかもしれない – vmg
を取得したいの提供:$配列が一致しました。これが彼らの目的です。 –