0

I want to fetch the value 10 from video[10]. How to do this using regular expression? I have tried : /video\[(.*?)\]/. This did not work.

1
  • How did it not work? It works fine for me (e.g. /video\[(.*?)\]/.match("video[10]")[1] gives "10"). Commented Nov 19, 2015 at 16:12

2 Answers 2

1

It should be /video\[(.*)\]/.

/video\[(.*)\]/.match "video[10]"
#=> #<MatchData "video[10]" 1:"10">
Sign up to request clarification or add additional context in comments.

1 Comment

You need to make .* non-greedy: /video\[(.*)\]/.match "video[10]]]]" #=> #<MatchData "video[10]]]]" 1:[10]]]">.
0
r = /
    \b      # match a word break
    video\[ # match string
    (\d+)   # match >= 1 digits in capture group 1
    \]      # match char
    /x      # extended/free-spacing mode

"Martha, where did you put video[10]?"[r,1]
  #=> "10"

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.