从 r 中的 url 中提取字符串
Extract string from url in r
我的数据框包含一个 URL 字段,该字段有时包含一个 13 位产品标识符。我需要提取此产品 ID 并将其写入名为 ISBN 的新列。以下是 3 个不同的 URL,每个的产品 ID 位置不同:
>https://catalog.macmillan.com/childrens/book/brazen/rebel-ladies-who-rocked-the-world/pnlope-bagieu/**9781626728691**?utm_source=exacttarget&utm_medium=newsletter&utm_term=na-schoolandlibrary&utm_content=na-discover-nl&utm_campaign=schoolandlibrary
>https://us.macmillan.com/excerpt?isbn=**9781250151025**&utm_source=exacttarget&utm_medium=newsletter&utm_term=na-schoolandlibrary&utm_content=na-discover-nl&utm_campaign=schoolandlibrary
>https://catalog.macmillan.com/childrens/book/so-tall-within/sojourner-truths-long-walk-toward-freedom/gary-d-schmidt/daniel-minter/**9781626728721**?utm_source=exacttarget&utm_medium=newsletter&utm_term=na-schoolandlibrary&utm_content=na-discover-nl&utm_campaign=schoolandlibrary
使用gregexpr
,假设产品编号的长度始终为13,如图所示。
regmatches(tx, gregexpr("(\d{13})", tx))
# [[1]]
# [1] "9781626728691" "9781250151025" "9781626728721"
数据
tx <- "https://catalog.macmillan.com/childrens/book/brazen/rebel-ladies-who-rocked-the-world/pnlope-bagieu/9781626728691?utm_source=exacttarget&utm_medium=newsletter&utm_term=na-schoolandlibrary&utm_content=na-discover-nl&utm_campaign=schoolandlibrary https://us.macmillan.com/excerpt?isbn=9781250151025&utm_source=exacttarget&utm_medium=newsletter&utm_term=na-schoolandlibrary&utm_content=na-discover-nl&utm_campaign=schoolandlibrary https://catalog.macmillan.com/childrens/book/so-tall-within/sojourner-truths-long-walk-toward-freedom/gary-d-schmidt/daniel-minter/9781626728721?utm_source=exacttarget&utm_medium=newsletter&utm_term=na-schoolandlibrary&utm_content=na-discover-nl&utm_campaign=schoolandlibrary"
我的数据框包含一个 URL 字段,该字段有时包含一个 13 位产品标识符。我需要提取此产品 ID 并将其写入名为 ISBN 的新列。以下是 3 个不同的 URL,每个的产品 ID 位置不同:
>https://catalog.macmillan.com/childrens/book/brazen/rebel-ladies-who-rocked-the-world/pnlope-bagieu/**9781626728691**?utm_source=exacttarget&utm_medium=newsletter&utm_term=na-schoolandlibrary&utm_content=na-discover-nl&utm_campaign=schoolandlibrary
>https://us.macmillan.com/excerpt?isbn=**9781250151025**&utm_source=exacttarget&utm_medium=newsletter&utm_term=na-schoolandlibrary&utm_content=na-discover-nl&utm_campaign=schoolandlibrary
>https://catalog.macmillan.com/childrens/book/so-tall-within/sojourner-truths-long-walk-toward-freedom/gary-d-schmidt/daniel-minter/**9781626728721**?utm_source=exacttarget&utm_medium=newsletter&utm_term=na-schoolandlibrary&utm_content=na-discover-nl&utm_campaign=schoolandlibrary
使用gregexpr
,假设产品编号的长度始终为13,如图所示。
regmatches(tx, gregexpr("(\d{13})", tx))
# [[1]]
# [1] "9781626728691" "9781250151025" "9781626728721"
数据
tx <- "https://catalog.macmillan.com/childrens/book/brazen/rebel-ladies-who-rocked-the-world/pnlope-bagieu/9781626728691?utm_source=exacttarget&utm_medium=newsletter&utm_term=na-schoolandlibrary&utm_content=na-discover-nl&utm_campaign=schoolandlibrary https://us.macmillan.com/excerpt?isbn=9781250151025&utm_source=exacttarget&utm_medium=newsletter&utm_term=na-schoolandlibrary&utm_content=na-discover-nl&utm_campaign=schoolandlibrary https://catalog.macmillan.com/childrens/book/so-tall-within/sojourner-truths-long-walk-toward-freedom/gary-d-schmidt/daniel-minter/9781626728721?utm_source=exacttarget&utm_medium=newsletter&utm_term=na-schoolandlibrary&utm_content=na-discover-nl&utm_campaign=schoolandlibrary"