2

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 $

1
  • You will have to post process these matches anyway since you can't skip a part of a match. Easiest is to use (\d[\d\s]*\p{Sc})\sin\s(\p{Sc}) and remove whitespace from Group 1. Commented May 15, 2017 at 14:57

2 Answers 2

2
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
Sign up to request clarification or add additional context in comments.

3 Comments

This seem to work, but only going for one match. If I have 12£ in $ - 4$ in £ it'll only return first pair
@MaximFedotov use scan and map instead.
@MaximFedotov - You can do that with a scan: sentence.scan(/[^-+*\/]*in[^-+*\/]*/).map { |el| el.to_s.strip.split(/ *in */) }
0
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 [+*\/-].

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.