簡體   English   中英

隔離文本中的特定數值

[英]Isolating specific numeric values in text

我有大量文本要在各種屬性的文本 csv 文件上進行搜索,以查找屬性的平方米數值實際上是多少。 例如:

string <- "This is a wonderful 120 sqm flat with a stunning view"

我知道我可以使用以下內容來提取數值:

sqm <- as.numeric(gsub("\\D", "", string)) 

它應該返回一個“120”的數字向量。 但是,我想知道是否有更復雜的方法來實現這一點,因為文本中可能存在其他不相關的數值?

有什么方法可以搜索“平方米”並返回它前面的數字? 非常感謝您的任何評論。

我相信這個正則表達式前瞻應該有效:

library(stringr)
##
string <- "This is a wonderful 120 sqm flat with a stunning view"
re <- "((\\d+)(?=\\s?sqm))"
##
R> str_extract(string, perl(re))
[1] "120"

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM