PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.ruby > needed using a regex .sub
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
needed using a regex .sub

Réponse
 
LinkBack Outils de la discussion
Vieux 22/11/2007, 13h33   #1
Peter Vanderhaden
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut needed using a regex .sub

I'm trying to insert a NULL between the first pair of commas in the line
below if it's not already there.

Before: PWCLLP-243-00000002,,Images\1\pwcllp_243_00000002.tif,,,,

After: PWCLLP-243-00000002,NULL,Images\1\pwcllp_243_00000002.tif,,,,

I want to replace only the first instance of ,, on the line with ,NULL,

I can do that with: @record.sub!(',,',',NULL,')

The problem is if the record already has NULL between those 2 commas,
I'd get:

PWCLLP-243-00000002,NULL,Images\1\pwcllp_243_00000002.tif,NUL L,,,

I tried this: @record.sub!('\[[:digit:]],,',',NULL,')

I figured that would replace the first instance of a digit followed by 2
commas, but it doesn't work. I'm assuming my syntax is incorrect, but
I've tried all variations I can think of. Anyone out there have any
ideas? I'd be very grateful!
PV
--
Posted via http://www.ruby-forum.com/.

  Réponse avec citation
Vieux 22/11/2007, 15h27   #2
Alex LeDonne
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: needed using a regex .sub

On Nov 22, 2007 6:33 AM, Peter Vanderhaden <bostonantifan@yahoo.com> wrote:
> I'm trying to insert a NULL between the first pair of commas in the line
> below if it's not already there.
>
> Before: PWCLLP-243-00000002,,Images\1\pwcllp_243_00000002.tif,,,,
>
> After: PWCLLP-243-00000002,NULL,Images\1\pwcllp_243_00000002.tif,,,,
>
> I want to replace only the first instance of ,, on the line with ,NULL,
>
> I can do that with: @record.sub!(',,',',NULL,')
>
> The problem is if the record already has NULL between those 2 commas,
> I'd get:
>
> PWCLLP-243-00000002,NULL,Images\1\pwcllp_243_00000002.tif,NUL L,,,
>
> I tried this: @record.sub!('\[[:digit:]],,',',NULL,')
>
> I figured that would replace the first instance of a digit followed by 2
> commas, but it doesn't work. I'm assuming my syntax is incorrect, but
> I've tried all variations I can think of. Anyone out there have any
> ideas? I'd be very grateful!
> PV


You might want to use a CSV library, like FasterCSV. But if you don't
want that overhead and really want to use a regex...

When you say the "first instance of ,,", do you really mean "only if
the first comma on the line is immediately followed by another comma"?

@record.sub!(/^([^,]*),,/, '\1,NULL,')

That regex is: Start at the beginning of the line, match all the
non-comma characters, followed by 2 commas.
The replacement is: All the non-comma characters, then ',NULL,'
instead of the two commas.

-Alex

  Réponse avec citation
Vieux 23/11/2007, 12h25   #3
Peter Vanderhaden
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: needed using a regex .sub

Alex,
Your solution is exactly what I need, thank you very much! I'm getting
better with regular expressions the more I work with them, and I really
appreciate your explanation of this regex.
PV

Alex LeDonne wrote:
> On Nov 22, 2007 6:33 AM, Peter Vanderhaden <bostonantifan@yahoo.com>
> wrote:
>>
>> ideas? I'd be very grateful!
>> PV

>
> You might want to use a CSV library, like FasterCSV. But if you don't
> want that overhead and really want to use a regex...
>
> When you say the "first instance of ,,", do you really mean "only if
> the first comma on the line is immediately followed by another comma"?
>
> @record.sub!(/^([^,]*),,/, '\1,NULL,')
>
> That regex is: Start at the beginning of the line, match all the
> non-comma characters, followed by 2 commas.
> The replacement is: All the non-comma characters, then ',NULL,'
> instead of the two commas.
>
> -Alex


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

  Réponse avec citation
Vieux 23/11/2007, 19h04   #4
7stud --
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: needed using a regex .sub

Peter Vanderhaden wrote:
> Alex,
> Your solution is exactly what I need, thank you very much! I'm getting
> better with regular expressions the more I work with them, and I really
> appreciate your explanation of this regex.
> PV


On my system, this solution is 80% faster, and if your strings get
longer it will be faster still:

str = 'PWCLLP-243-00000002,,Images\1\pwcllp_243_00000002.tif,,,,'

first_comma_pos = str.index(',')
next_char_pos = first_comma_pos + 1

if str[next_char_pos] == ?,
str.insert(next_char_pos, 'NULL')
end

puts str


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

  Réponse avec citation
Vieux 23/11/2007, 19h10   #5
7stud --
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: needed using a regex .sub

7stud -- wrote:
> On my system, this solution is 80% faster, and if your strings get
> longer it will be faster still:
>
> first_comma_pos = str.index(',')


...and twice as fast with this additional change:

first_comma_pos = str.index(?,)
--
Posted via http://www.ruby-forum.com/.

  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 17h13.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,11871 seconds with 13 queries