Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails - extract substring with in [ and ] from string

This works.

"<name> <substring>"[/.*<([^>]*)/,1]
=> "substring"

But I want to extract substring within [ and ].

input:

string = "123 [asd]"

output:

asd

Anyone can help me?


1 Answers

You can do:

"123 [asd]"[/\[(.*?)\]/, 1]

will return

"asd"

You can test it here: https://rextester.com/YGZEA91495

like image 184
nuaky Avatar answered Nov 18 '25 09:11

nuaky



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!