Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
686 views
in Technique[技术] by (71.8m points)

regex - Extracting a number following specific text in R

I have a data frame which contains a column full of text. I need to capture the number (can potentially be any number of digits from most likely 1 to 4 digits in length) that follows a certain phrase, namely 'Floor Area' or 'floor area'. My data will look something like the following:

"A beautiful flat on the 3rd floor with floor area: 50 sqm and a lift"
"Newbuild flat. Floor Area: 30 sq.m" 
"6 bed house with floor area 50 sqm, lot area 25 sqm"

If I try to extract just the number or if I look back from sqm I will sometimes get the lot area by mistake.If someone could help me with a lookahead regex or similar in stringr, I'd appreciate it. Regex is a weak point for me. Many thanks in advance.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

A common technique to extract a number before or after a word is to match all the string up to the word or number or number and word while capturing the number and then matching the rest of the string and replacing with the captured substring using sub:

# Extract the first number after a word:
as.integer(sub(".*?<WORD_OR_PATTERN_HERE>.*?(\d+).*", "\1", x))

# Extract the first number after a word:
as.integer(sub(".*?(\d+)\s*<WORD_OR_PATTERN_HERE>.*", "\1", x))

NOTE: Replace \d+ with \d+(?:\.\d+)? to match int or float numbers (to keep consistency with the code above, remember change as.integer to as.numeric). \s* matches 0 or more whitespace in the second sub.

For the current scenario, a possible solution will look like

v <- c("A beautiful flat on the 3rd floor with floor area: 50 sqm and a lift","Newbuild flat. Floor Area: 30 sq.m","6 bed house with floor area 50 sqm, lot area 25 sqm")
as.integer(sub("(?i).*?\bfloor area:?\s*(\d+).*", "\1", v))
# [1] 50 30 50

See the regex demo.

You may also leverage a capturing mechanism with str_match from stringr and get the second column value ([,2]):

> library(stringr)
> v <- c("A beautiful flat on the 3rd floor with floor area: 50 sqm and a lift","Newbuild flat. Floor Area: 30 sq.m","6 bed house with floor area 50 sqm, lot area 25 sqm")
> as.integer(str_match(v, "(?i)\bfloor area:?\s*(\d+)")[,2])
[1] 50 30 50

See the regex demo.

The regex matches:

  • (?i) - in a case-insensitive way
  • \bfloor area:? - a whole word ( is a word boundary) floor area followed by an optional : (one or zero occurrence, ?)
  • \s* - zero or more whitespace
  • (\d+) - Group 1 (will be in [,2]) capturing one or more digits

See R demo online


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...