2

I have this simple little string "Wed 09 Sept", and I'd like to select "09 Sept"

This will take everything from the 1st space:

\s(.*) (as from Regex to get everything after the first space)

But it comes with the space, and i'd like to get rid of that

Community
  • 1
  • 1
Zach Smith
  • 8,458
  • 13
  • 59
  • 133

3 Answers3

5

Get the first capture group of that regular expression:

"Wed 09 Sept" =~ /\s(.*)/ 
selection     = $1
# => "09 Sept"

The =~ operator matches a pattern against a string. The $1, $2, $3, etc. variables refer to capture groups.

August
  • 12,410
  • 3
  • 35
  • 51
  • Thanks. I'm not sure what a capture group is or how to implement that – Zach Smith Jan 17 '15 at 13:49
  • It's already in the regular expression you posted. More information: http://www.regular-expressions.info/brackets.html – August Jan 17 '15 at 13:50
  • This is what I'm looking for I think. What is that "=~" sign? Also, the $ sign is a global variable right? What is happening in this code? – Zach Smith Jan 17 '15 at 14:04
  • If you're doing this in a method you can write it like this `selection[/\s(.*)/][$1]` – Jake Apr 12 '19 at 18:41
0

You could use \K to discard the previously matched characters.

^\S*\s\K.*

OR

^\S* \K.*

OR

\s\K.*

OR

 \K.*

\S* matches zero or more non-space characters and \s+ matches one or more spaces. \K discards the previously matched characters that from being printed out at the final. Now .* would match all the remaining characters. This would be printed at the final.

DEMO 1 DEMO 2

Avinash Raj
  • 172,303
  • 28
  • 230
  • 274
0

Group the first space and the rest /(\s)(.*)/, like:

pry(main)> m = /(?'first'\s)(?'rest'.*)/.match 'Wed 09 Sept'
=> #<MatchData " 09 Sept" first:" " rest:"09 Sept">
pry(main)> m[:rest]
=> "09 Sept"
Grych
  • 2,861
  • 13
  • 22