Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8871689
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T18:03:10+00:00 2026-06-14T18:03:10+00:00

I was wondering how to select words near each other using regular expressions. For

  • 0

I was wondering how to select words near each other using regular expressions.
For example, I would like to select the digits and the word miles from the following phrases:

"140,000 mostly freeway miles" 
"173k commuter miles. "       
"154K(all highway) miles

I don’t know how to fill in for the optional words in the middle:

[0-9]+ ???? miles

*near could be defined as 1-3 words apart. Thanks for pointing that out.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T18:03:12+00:00Added an answer on June 14, 2026 at 6:03 pm

    Here is an answer in R. The other answers could work with some modification. Mostly, they need to have “double escapes” and you will have to use the paired functions regexpr and regmatches.

    x=c("140,000 mostly freeway miles" ,"173k commuter miles. " ,"154K(all highway) miles")
    
    
    gsub('([[:digit:][:punct:]k]+).*(miles).*', 
         '\\1 \\2', 
         x,
         ignore.case=TRUE)
    
    # [1] "140,000 miles" "173k miles"    "154 miles"    
    

    This says group numbers punctuation or a k in group 1. Follow this by anything. Then this is followed by group 2 which is the word miles, followed by anything else.

    You could also use the “normal” regex syntax:

    gsub('([0-9,k]+).*(miles).*', 
         '\\1 \\2', 
         x,
         ignore.case=TRUE)
    

    However, I would clean up the data first then do some simpler matching! (e.g. tolower and remove punctuation).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I was wondering if anybody knows how to select using js the complete table,
I'm wondering if there is any particular performance advantage in using LIKE over NOT
I was wondering how exactly inner joins works in mysql. If I do SELECT
I was wondering how I can select the output device for audio in directshow.
I was wondering if there was a way to select only certain values in
i was just wondering if anyone knows how to select rows where a specified
I'm wondering if it's possible to use a wildcard to select multiple cookies. I'm
i was wondering if jquery can be used to manipulate my select html element
I was wondering if there is any way to SELECT the first number that
I'm wondering if it's possible to change the order of things in a select

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.