# Help needed using a regex .sub

**URL:** <https://www.ruby-forum.com/t/help-needed-using-a-regex-sub/120996>\
**Category:** Ruby\
**Created:** [November 22, 2007, 2:33pm UTC](https://www.ruby-forum.com/t/help-needed-using-a-regex-sub/120996 "2007-11-22T14:33:27Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![wpvanpaassen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/6f9a4e/32.png) [@wpvanpaassen](https://www.ruby-forum.com/u/wpvanpaassen)\
**Post date:** [November 22, 2007, 2:33pm UTC](https://www.ruby-forum.com/t/help-needed-using-a-regex-sub/120996/1 "2007-11-22T14:33:27Z")

</div>

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,NULL,

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

---

<div class="post-metadata">

**Author:** ![wpvanpaassen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/6f9a4e/32.png) [@wpvanpaassen](https://www.ruby-forum.com/u/wpvanpaassen)\
**Post date:** [November 22, 2007, 4:28pm UTC](https://www.ruby-forum.com/t/help-needed-using-a-regex-sub/120996/2 "2007-11-22T16:28:43Z")

</div>

On Nov 22, 2007 6:33 AM, Peter V. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
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

---

<div class="post-metadata">

**Author:** ![wpvanpaassen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/6f9a4e/32.png) [@wpvanpaassen](https://www.ruby-forum.com/u/wpvanpaassen)\
**Post date:** [November 23, 2007, 1:25pm UTC](https://www.ruby-forum.com/t/help-needed-using-a-regex-sub/120996/3 "2007-11-23T13:25:21Z")

</div>

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 V. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> 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

---

<div class="post-metadata">

**Author:** ![wpvanpaassen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/6f9a4e/32.png) [@wpvanpaassen](https://www.ruby-forum.com/u/wpvanpaassen)\
**Post date:** [November 23, 2007, 8:10pm UTC](https://www.ruby-forum.com/t/help-needed-using-a-regex-sub/120996/4 "2007-11-23T20:10:35Z")

</div>

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(?,)

---

<div class="post-metadata">

**Author:** ![wpvanpaassen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/6f9a4e/32.png) [@wpvanpaassen](https://www.ruby-forum.com/u/wpvanpaassen)\
**Post date:** [November 23, 2007, 8:04pm UTC](https://www.ruby-forum.com/t/help-needed-using-a-regex-sub/120996/5 "2007-11-23T20:04:48Z")

</div>

Peter V. 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
