Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

182
Views
Break string into words using scan method + regexp, if word has `'` character, drop this character and everything after it
sample_string = "let's could've they'll you're won't"
sample_string.scan(/\w+/)

Above gives me:

["let", "s", "could", "ve", "they", "ll", "you", "re", "won", "t"]

What I want:

["let", "could", "they", "you", "won"]

Been playing around in https://rubular.com/ and trying assertions like \w+(?<=') but no luck.

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

Given:

> sample_string = "let's could've they'll you're won't"

You can do split and map:

> sample_string.split.map{|w| w.split(/'/)[0]}
=> ["let", "could", "they", "you", "won"]
over 4 years ago · Santiago Trujillo Report

0

You can use

sample_string.scan(/(?<![\w'])\w+/)
sample_string.scan(/\b(?<!')\w+/)

See the Rubular demo. The patterns (they are absolute synonyms) match

  • (?<![\w']) - a location in the string that is not immediately preceded with a word or ' char
  • \b(?<!') - a word boundary position which is not immediately preceded with a ' char
  • \w+ - one or more word chars.

See the Ruby demo:

sample_string = "let's could've they'll you're won't"
p sample_string.scan(/(?<![\w'])\w+/)
# => ["let", "could", "they", "you", "won"]
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!