Maxim Fedotov
Maxim Fedotov

Reputation: 1357

Regex for selecting substrings before and after a string

I am trying to find a right regex expression to select substrings between another substring, which I'd like to exclude. For example in this string:

11 - 12£ in $ + 13

I want to select 12£ and $. Basically, it's substrings around in, until I hit an array of values I want to use as end/start, in this case, arithmetic operators %w(+ - / *)

So far closest I got was using this regex /(.\d\p{Sc})\sin\s(\p{Sc})/

Some more examples:

10 - 12$ in £ - 13$ should return 12$ and £

12 $ in £ should return 12$ and £

100£in$ should return 100£ and $

Upvotes: 2

Views: 130

Answers (2)

Cary Swoveland
Cary Swoveland

Reputation: 110685

r = /
    \s+[+*\/-]\s+ # match 1+ whitespaces, 1 char in char class, 1+ whitespaces
    (\S+)         # match 1+ non-whitespaces in capture group 1
    \s+in\s+      # match 1+ whitespaces, 'in', 1+ whitespaces
    (\S+)         # match 1+ non-whitespaces in capture group 2
    \s+[+*\/-]\s  # match 1+ whitespaces, 1 char in char class, 1+ whitespaces
    /x            # free-spacing regex definition mode

str = '11 -     12£ in $ + 13 / 13F in   % * 4'
str.scan(r)
  #=> [["12£", "$"], ["13F", "%"]] 

See the doc for String#scan to see how scan handles capture groups.

Note that '-' must be first or last in the character class [+*\/-].

Upvotes: 0

ndnenkov
ndnenkov

Reputation: 36100

sentence.match(/[^-+*\/]*in[^-+*\/]*/).to_s.strip.split(/ *in */)
  • [^-+*\/]* matches multiple non-arithmetic operators
  • this will hence get everything from the "opening" to the "closing" operator that surround an in
  • #strip removes the leading and trailing whitespaces
  • finally, split into two strings, removing in and the spaces around it

Upvotes: 2

Related Questions