Parsing String Data

B

Bob Theslob

I am very new to Ruby and I have an issue that I cannot seem to
solve.

Parse a data string, excise four digits (non- adjoined) in the middle
of the string, and then assemble a second string using the data.

As an example, my fisrt string has text =E2=80=9CTest Part 123 G 477=E2=80=
=9D, I want to
assemble a new string =E2=80=9CAABB123477=E2=80=9D.

What I do not know how to do is parse the text to get the numbers. Once
having them I have had no problems assembling the new string.

Can you help with this?

-- =

Posted via http://www.ruby-forum.com/.=
 
J

Jesús Gabriel y Galán

I am very new to Ruby and I have an issue that I cannot seem to
solve.

Parse a data string, excise four digits (non- adjoined) =A0in the middle
of the string, and then assemble a second string using the data.

As an example, my fisrt string has text =93Test Part 123 G 477=94, I want= to
assemble a new string =A0=93AABB123477=94.

What I do not know how to do is parse the text to get the numbers. =A0Onc= e
having them I have had no problems assembling the new string.

Can you help with this?

You can use String#split or a regex depending on the requirements:

irb(main):001:0> parts =3D "Test Part 123 G 477".split(" ")
=3D> ["Test", "Part", "123", "G", "477"]
irb(main):002:0> parts[2]
=3D> "123"
irb(main):003:0> parts[4]
=3D> "477"

If you know the data is always separated by spaces and the number is
in the second and fourth places, this should work.

Jesus.
 
A

Ammar Ali

I am very new to Ruby and I have an issue that I cannot seem to
solve.

Parse a data string, excise four digits (non- adjoined) =C2=A0in the midd= le
of the string, and then assemble a second string using the data.

As an example, my fisrt string has text =E2=80=9CTest Part 123 G 477=E2= =80=9D, I want to
assemble a new string =C2=A0=E2=80=9CAABB123477=E2=80=9D.

What I do not know how to do is parse the text to get the numbers. =C2=A0= Once
having them I have had no problems assembling the new string.

If you're only interested in the numbers, using scan is another option:
=3D> ["123", "477"]

HTH,
Ammar
 
B

Bob Theslob

t =

#961831:
having them I have had no problems assembling the new string.

Can you help with this?

You can use String#split or a regex depending on the requirements:

irb(main):001:0> parts =3D "Test Part 123 G 477".split(" ")
=3D> ["Test", "Part", "123", "G", "477"]
irb(main):002:0> parts[2]
=3D> "123"
irb(main):003:0> parts[4]
=3D> "477"

If you know the data is always separated by spaces and the number is
in the second and fourth places, this should work.

Jesus.

Jesus,

Thank you, I cannot guarantee that the data will always be in the same =

format, however I know that the length of the string is always the same. =

Can I pase the string one character at a time, and apply a set of rules =

to determine what data to use and what data to discard?

For example I know that I want to use only the numerical data and not =

the alphabetic data. The simplest, though not elegant way I can think of =

is to go one character at a time and determine if that character is a =

number or a letter.

Bob

-- =

Posted via http://www.ruby-forum.com/.=
 
W

w_a_x_man

I am very new to Ruby and I have an issue that I cannot seem to
solve.

Parse a data string, excise four digits (non- adjoined)  in the middle
of the string, and then assemble a second string using the data.

As an example, my fisrt string has text “Test Part 123 G 477”, I wantto
assemble a new string  “AABB123477”.

What I do not know how to do is parse the text to get the numbers.  Once
having them I have had no problems assembling the new string.

"Test Part 12 or 3 G 477".gsub( /\D/, "" )
==>"123477"
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,058
Latest member
QQXCharlot

Latest Threads

Top