iT邦幫忙

0

aspnet程式問題

請教大家
要如何抓出一段文字中指定的參數spaceKey後的參數呢?spaceKey後的參數會有2-6碼的文字,接著使用&符號來串接其他參數,資料儲存的內容如下

/plugins/editor-loader/editor.action?parentPageId=&pageId=47120991&spaceKey=NN&atl_after_login_redirect=%2Fpages%2Fviewpage.action&timeout=12000&_=1498533190644 HTTP/1.1 43742 200 http-nio-8090-exec-9

謝謝

小魚 iT邦大師 1 級 ‧ 2017-06-30 23:55:29 檢舉
請問上面這一串是網址嗎?還是?
你是要抓網址後面的參數,還是要從一堆字串裡面抓出這些字來?
ektrontek iT邦研究生 1 級 ‧ 2017-07-01 07:01:38 檢舉
我從LOG轉存到DB的一串文字,會有多筆這樣的文字,來抓出每一筆的spaceKey值來做運算加總
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

2 個回答

0
小魚
iT邦大師 1 級 ‧ 2017-07-01 09:10:53

給你一個範例

using System.Text.RegularExpressions;

... ...

string input = "/plugins/editor-loader/editor.action?parentPageId=&pageId=47120991&spaceKey=NN&atl_after_login_redirect=%2Fpages%2Fviewpage.action&timeout=12000&_=1498533190644 HTTP/1.1 43742 200 http-nio-8090-exec-9";
string pattern = @"spaceKey=(?<word>[\s\S]+?)&";
Regex regex = new Regex(pattern, RegexOptions.IgnoreCase);
MatchCollection matches = regex.Matches(input); // 將比對後集合傳給 MatchCollection
int index = 0;
foreach (Match match in matches) // 一一取出 MatchCollection 內容
{
      // 將 Match 內所有值的集合傳給 GroupCollection groups
      GroupCollection groups = match.Groups;
      // 印出 Group 內 word 值
      Console.WriteLine(++index + ": " + groups["word"].Value.Trim());
}

執行出來的結果就是 1 NN

其中groups["word"].Value.Trim()取出來的就會是你要的值,
雖然我用了foreach,不過你這個例子應該只會抓到一個值而已,
如果要了解深一點
請搜尋 Regex 和 正規表示式

看更多先前的回應...收起先前的回應...
ektrontek iT邦研究生 1 級 ‧ 2017-07-03 21:41:46 檢舉

請教前輩
我參考MSDN VB的範例,修改如下結果

  If Not IsPostBack Then
            Dim strDbCon As String = "Data Source=(local);Initial Catalog=DB14017;Persist Security Info=True;User ID=sa;Password=5342601water"
            Dim con As New SqlConnection(strDbCon)
            Dim ad As New SqlDataAdapter("SELECT [f1],[f2] FROM [DB].[dbo].[5] where f2 like '%viewpage%'", con)
            Dim dt As New DataTable
            ad.Fill(dt)
            If dt.Rows.Count > 0 Then
                Label1.Text = IIf(dt.Rows(0).IsNull("f2"), "", dt.Rows(0)("f2"))
                Dim pattern As String = "spaceKey=(?<word>[\s\S]+?)&"
                Dim regex As Regex = New Regex(pattern, RegexOptions.IgnoreCase)
                
                Dim matches As MatchCollection = regex.Matches(dt.Rows(0).IsNull("f2"))
                Dim index As Integer = 0
              
                For Each m As Match In regex.Matches(Match, pattern)
                    Console.WriteLine("'{0}' found at index {1}.", m.Value, m.Index)
                Next
            End If

但會出現下列的訊息,是否有建議的方向呢?謝謝
http://ithelp.ithome.com.tw/upload/images/20170703/20003705YFJVR0TeoW.jpg

小魚 iT邦大師 1 級 ‧ 2017-07-03 22:06:06 檢舉

1.VB應該跟C#差不多,照我上面的例子應該用Match In matches就可以了,你前面已經執行過一次後面不用再重複了,而且Match是類別不是變數,你應該帶入變數。
2.另外你可以用一個string接收f2的值,然後下中斷點看看f2的值跟你要的有沒有一樣,如果一樣再處理後面的問題。
3.我直接執行你的Value跟Index會把外面你不要的字串也帶進來,建議用我上面的方法 m.Groups["word"].Value.Trim() 抓值就可以了。

ektrontek iT邦研究生 1 級 ‧ 2017-07-20 22:12:58 檢舉

感謝協助

小魚 iT邦大師 1 級 ‧ 2017-07-20 22:22:59 檢舉

不用客氣,
如果有幫助到你可以幫忙給給最佳解答,
以後別人要找答案也會比較容易,
感恩~

0
sam0407
iT邦大師 1 級 ‧ 2017-07-01 09:14:05

先用String.IndexOf找出空白所在的字串的位置
https://msdn.microsoft.com/zh-tw/library/k8b1470s(v=vs.110).aspx
再用String.Substring取出您所要2~6碼長度的字串
https://msdn.microsoft.com/zh-tw/library/aka44szs(v=vs.110).aspx

我要發表回答

立即登入回答