PDA

View Full Version : text parsing


Bret
08-07-2008, 07:18 PM
I need to do some text parsing, pretty basic stuff.

ie.

some crap at the top

ID: 135 V: 1
ID: 1 V:1
ID: 12 V:2
ID: 41 V:2

few more different rows at the bottom id like to parse

----

Ideally it would be from a web form post but parsing from a text file work as well. I am going to be inserting it into a mysql/sqllite database.

I know of some ways to do it, ive search around, some good info, some outdated.

Wanted to know if someone had a good starting point or some suggestions. I can prolly figure it out using some random technique or class or something but I wanted to post and see if someone could point be in a good direction so I dont waste too much time dicking around.

php would prolly be ideal but im up for whatever is easiest and fits my needs would work.

think I will be parsing some xml as well, so any pointers for that would be helpful

Bonzo
08-07-2008, 08:29 PM
I've done parsing a few times, but nothing advanced. I mean, do you just want what is on the entire line or what?

Bret
08-07-2008, 08:34 PM
eh I have been fucking around with regular expressions, prolly what ill need to do either way I imagine.. so help with that would be nice

I want to extract the numbers and enter them into a database, so in the example

ID: 135 V: 1
ID: 1 V: 1
ID: 12 V: 2
ID: 41 V: 2

I want 135 to be placed into the column ID, and 1 in the column Version and so on

Bonzo
08-07-2008, 09:03 PM
Ok, so just a regex to grab numbers only. Unless its more complicated than that?

lornfang
08-08-2008, 06:55 AM
this is a pcre, might need to be modified based on your implementation.

m/ID:\ (\d+)\ V:\ (\d+)/

then you would extract $1 and $2 as 135 and 1. () basically captures the match it encloses and assigns it to a position parameter. Like I said, this is perl, other languages can be slightly different.

Hopefully that will get you started.

Bonzo
08-08-2008, 04:18 PM
That sounds like that shell script you helped me with.

lornfang
08-08-2008, 05:19 PM
That sounds like that shell script you helped me with.

might be, I don't remember what we did in it. lol.

Bret
08-08-2008, 05:44 PM
thanks lorn, I have the exact file(text) I need to parse out, will prolly play with it tomorrow

lornfang
08-08-2008, 06:35 PM
alright, cool. If you run into some trouble send it to me and I'll see if I can whip something up in perl for you.