Hello, I need some help with regex
I have the following expression: “Laptop MARCOCHEN (192.168.5.73)”
I need a regular express to capture only the second word, in this case
“MARCOCHEN”. I don’t want to use “split”
So far I am using / \S+ /i
but this is getting also the spaces before and after so what I am
getting is " MARCOCHEN "
You are getting that because you have spaces in it. You can get the same
result if you use /(\S+)/ and using the $1 variable, which should
contain
“MARCOCHEN”.
Is there a way to do it so that the $& will contain “MARCOCHEN”?
Isaac S. wrote in post #1024664:
You are getting that because you have spaces in it. You can get the same
result if you use /(\S+)/ and using the $1 variable, which should
contain
“MARCOCHEN”.
On Mon, Oct 3, 2011 at 9:06 AM, Eyal Eizenberg [email protected] wrote:
Is there a way to do it so that the $& will contain “MARCOCHEN”?
Isaac S. wrote in post #1024664:
You are getting that because you have spaces in it. You can get the same
result if you use /(\S+)/ and using the $1 variable, which should
contain
“MARCOCHEN”.
$~ has the matched result from the most recent regexp. Convert to a string
thusly
($~).to_s
You’re point being? Why would I bother to use $~.to_s if I can
directly use $& and get the same? Also, my point was specifically
that you can get away without those global variables and instead use
what String#[] returns.