Regex an array?

I am having troubles trying to regex the following array to only include
the last word

[“user created TI: TI-0001”]

I am trying to regex or gsub so only TI-0001 returns.

This is probably not the best way of doing this but here goes :

/(\S*[^"]]){4}/

Will match ‘TI-0001’. If using on different strings that are not alike
it probably won’t work.

Look at http://www.rubular.com/ for regex in ruby. Great tool!

Will this help?

a = [“user created TI: TI-0001”]

print $+ if a[-1]=~/(\S+)$/ #TI-0001

[“user created TI: TI-0001”][0] =~ /.{7}$/
matched=$&

On Sat, 8 Oct 2011 03:59:50 +0900
skolo pen [email protected] wrote:

I am having troubles trying to regex the following array to only
include the last word

[“user created TI: TI-0001”]

I am trying to regex or gsub so only TI-0001 returns.

Is that opening string "user created TI: " the same every time?

Is ‘TI-0001’ the same length of data, every time?

If yes, then why a regex? What about something simple like this:

a = [ “user created TI: TI-0001” ]
b = a[0][ -7…-1 ]

On Fri, Oct 7, 2011 at 8:59 PM, skolo pen [email protected] wrote:

I am having troubles trying to regex the following array to only include
the last word

[“user created TI: TI-0001”]

I am trying to regex or gsub so only TI-0001 returns.

What exactly do you mean? Do you want to select all elements in the
Array which match? Or do you want to find the first that matches? Or
do you want to convert all elements in the Array to their matches?

Kind regards

robert

Josh C. wrote in post #1025685:

On Fri, Oct 7, 2011 at 1:59 PM, skolo pen [email protected] wrote:

This could change depending on what constitutes valid data, but it
solves
the example you’ve given.

array = [“user created TI: TI-0001”]
array.map { |line| line[/\S+$/] } # => [“TI-0001”]

Fascinating, this gives me the appropriate output, thanks for the
assistance all.

On Fri, Oct 7, 2011 at 1:59 PM, skolo pen [email protected] wrote:

This could change depending on what constitutes valid data, but it
solves
the example you’ve given.

array = [“user created TI: TI-0001”]
array.map { |line| line[/\S+$/] } # => [“TI-0001”]

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs