2
我需要能夠使用r中的名稱捕獲正則表達式中的組。我測試了本網站[Rd] Named capture in regexp中解釋的代碼,該示例沒有任何問題。我嘗試修改這些代碼來解決簡單的正則表達式。在正則表達式中命名捕獲
(xxxx)(?<id>\w{4})(?<number>\d{5})
欲瞭解更多詳情,請參閱該代碼here
我嘗試做r中
regex = "(xxxx) (?<id>[0-9A-Za-z]{4}) (?<number>[0-9]{5})"
notable = "xxxxcn0700814"
regexpr(regex,notable,perl = TRUE)
,這是我對這段代碼輸出
[1] -1
attr(,"match.length")
[1] -1
attr(,"useBytes")
[1] TRUE
attr(,"capture.start")
id number
[1,] -1 -1 -1
attr(,"capture.length")
id number
[1,] -1 -1 -1
attr(,"capture.names")
[1] "" "id" "number"
我可以看到這有什麼問題,因爲這個代碼類似於網頁的代碼。
在此先感謝
那麼究竟是什麼問題呢?我在輸出中看到捕獲的名稱。看起來你的正則表達式中有空格,但不在目標中。這是「問題」嗎? – MrFlick
在模式開始處添加'(?x)'或從模式中刪除空格。 –