2009-06-08 23 views
1

私は文字列と一致して、新しい文字列を作成するために、グループ化を使用しようとしている:マッチング正規表現

(let ((url (browse-url-url-at-point))) 
    (if (string-match "http://domain/\\([0-9]+\\)/\\([a-z]+\\)\.\\([0-9]+\\)" url) 
    (setq filename (concat (match-string 1 url) "_" (match-string 2) "." (match-string 3) ".xml")))) 

I(印刷URL)私は

"http://domain/1234/action.1234567" 
を以下の取得

I(印刷ファイル名)が成功した試合後、私は次のように取得する場合:

#("1234_ublish.eport s.xml" 0 5 nil 5 11 (face nxml-element-local-name-face fontified t) 11 12 nil 12 17 (face nxml-element-local-name-face fontified t) 17 18 (fontified t) 18 19 (face nxml-attribute-local-name-face fontified t) 19 23 nil) 

なぜこのhappingはありますか?

答えて

4

2番目と3番目の一致文字列にオプションの文字列パラメータを含めていません。マッチ文字列のドキュメントごとに、 "最後の検索が文字列の` string-match 'であった場合には文字列を与えなければなりません。

3

問題が見つかりました。

(文字列一致)は、元の文字列を引数として取る必要があります。そうでないと、奇妙な値を含む奇妙なリストが返されます。これまでのコードについての変更いずれにしても

、:

(let ((url (browse-url-url-at-point))) 
    (if (string-match "http://domain/\\([0-9]+\\)/\\([a-z]+\\)\.\\([0-9]+\\)" url) 
    (setq filename (concat (match-string 1 url) "_" (match-string 2 url) "." (match-string 3 url) ".xml")))) 

修正問題

2

mamboking already mentionedとして、match-stringのドキュメンテーション文字列は、すべてについて説明します:

(match-string NUM &optional STRING) 
⋮ 
STRING should be given if the last search was by `string-match' on STRING. 

あなたの場合もstring-matchのドキュメントをチェックすると、match-beginningとを使用することがわかりますマッチを取得します。これらは、ビルトインされている機能C.

(if (string-match "\\([a-z]\\)" "123 test string") 
    (match-beginning 1)) ;; 4 

でこれらの関数だけ一致したテキストの開始や終了の位置を返し、match-stringはあまりにも、元の文字列を必要とする理由です。 search-forwardまたはre-search-forwardを使用する場合、match-beginningmatch-endはバッファ位置を返すので、match-stringはバッファの内容から興味のある部分を簡単に部分文字列にすることができます。

match-string-no-propertiesを見てみると、match-stringと同じように動作し、テキストプロパティのない文字列を返します。