-1

I have one variable that comes from a map, I'm trying to get a specific part between square brackets

(e.g."dmfkdmfk[IWANTTHISPART]mlkm")

but it isn't working by the way I did. I'm trying as same way used here.

Original Code:

query_values = activities.map do |activity|
  '(' +
  "#{activity['note']}"
  +')'

end

I tried:

query_values = activities.map do |activity|
  '(' +
  "#{activity['note'].[/#{"["}(.*?)#{"]"}/m, 1]}" 
  +')'

end

Error log:

syntax error, unexpected '[', expecting '('
      '(' + "#{activity['note'].[/#{"["}(.*?)#{"]"}/m, 1]},""'" +')'
                                 ^
quase.rb:40: syntax error, unexpected keyword_end, expecting tSTRING_DEND

How can I go through? Many Thanks.

2
  • It's unclear what relevance the "Original Code" has to the question. Is that also non-working code that you tried? Keep n mind that we're not supposed to have to follow any links to understand the question, if there is additional information in the link that would clarify the question, it's best to edit that information into the question itself. Commented May 19, 2016 at 12:50
  • @WayneConrad Original code is the one working but not implemented the question issue. Commented May 19, 2016 at 16:57

4 Answers 4

6
str = "(dmfkdmfk[IWANTTHISPART]mlkm)"

#1 Use a regex with lookarounds

R0 = /
     (?<=\[) # match a left bracket in a positive lookbehind
     .+      # match one or more of any character
     (?=\])  # match a right bracket in a positive lookahead
     /x      # free-spacing regex definition mode

(same as R0 = /(?<=\[).+(?=\])/)

str[R0] #=> "IWANTTHISPART"

#2 Split string on a left or right bracket

R1 = /
     [\[\]] # match a left or right bracket
     /x

(same as R1 = /[\[\]]/)

str.split(R1)[1]
  #=> "IWANTTHISPART"

#3 No regex

str[str.index('[')+1..str.index(']')-1]
  #=> "IWANTTHISPART"
Sign up to request clarification or add additional context in comments.

1 Comment

Could also use scan here to find all instances of things inside square brackets.
2

You cannot have a period after the receiver when using [] in its syntax-sugar form. The following is ungrammatical:

string.[regex, parameter]

Use either the ordinary method invocation form:

string.[](regex, parameter)

or the syntax-sugar form:

string[regex, parameter]

4 Comments

But how can I use this? I'm not sure how to use the regex.
@JulinhodaAdelaide You already have a regex. Aren't you trying to use that? The parameter can follow the regex.
Does it work with map variable? I tried it all way but no success. Can you help me?
"#{activity['note'][/#{"["}(.*?)#{"]"}/m, 1]}"
1
/\[(.*)\]/.match( "dmfkdmfk[IWANTTHISPART]mlkm" )[1]
=> "IWANTTHISPART"                                                  

4 Comments

Should I use like : ""' /[(.*)]/.match( #{activity['type']} )[1]""' ??
@JulinhodaAdelaide, learn by trying! Your Rails console or IRB can answer that as fast as you can type it.
@JulinhodaAdelaide There's also Rubular which is handy for debugging regular expressions.
Good answer. A variant (which I don't like as well) is ("dmfkdmfk[IWANTTHISPART]mlkm" =~ /\[(.*)\]/) && $1.
1

You can use String#[] with a regular expression:

> a = "dmfkdmfk[IWANTTHISPART]mlkm"
> a[/\[.*?\]/][1..-2]
#=> "IWANTTHISPART"

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.