I am trying to parse a multi line string and get the rest of the line following a pattern.
text:
hello john your username is: jj thanks for signing up
I want to extract jj, aka everything after "your username is: "
One way:
text = "hello john\nyour username is: jj\nthanks for signing up\n"
match = text[/your username is: (.*)/]
value = $1
But this reminds me of perl... and doesn't "read" as naturally as I am told ruby should.
Is there a cleaner way? AKA A "ruby" way?
Thanks
Your code is pretty much the Ruby way. If you don't want to use the global $1
, you can use the 2 arg version String#[]
:
match = text[/your username is: (.*)/, 1]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With