Find and remove comma in four-digit numbers

154 views
Skip to first unread message

David Brostoff

unread,
Aug 24, 2016, 9:45:57 AM8/24/16
to textwr...@googlegroups.com
I am trying to remove the comma in four-digit numbers:

Find: 1,234

Replace with: 1234

I can find them with (,[0-9]) but how do I remove them?

Thank you,

David

David Brostoff

unread,
Aug 24, 2016, 9:46:04 AM8/24/16
to textwr...@googlegroups.com
To answer my own question, I found an answer here <http://stackoverflow.com/questions/11880651/find-replace-using-grep-and-textwrangler> that I modified.

Search: (\d{1,3}),(\d{1,3})

Replace: \1\2\3

This lets me find the pattern 1,234 and replace it with 1234.

David

Christopher Stone

unread,
Aug 24, 2016, 11:40:52 AM8/24/16
to TextWrangler-Talk
On Aug 23, 2016, at 21:48, David Brostoff <dav...@earthlink.net> wrote:
To answer my own question, I found an answer here <http://stackoverflow.com/questions/11880651/find-replace-using-grep-and-textwrangler> that I modified.

Search: (\d{1,3}),(\d{1,3})


Hey David,

That pattern fill find from 1 to 3 digits on each side of the comma, so strings like this will also be found:

1,1
1,12
12,1
22,22
333,333

You want to limit the number of digits found to 1 and 3 like so:

Find:

\b(\d{1}),(\d{3})\b

Replace:

\1\2

Using the word boundaries (\b) you keep it from finding embedded strings like:

11,234

You can also use backward and forward looking assertions to find only the comma between the patterns:

Find:

(?<=\b\d),(?=\d{3}\b)

Replace:

Nothing.

--
Best Regards,
Chris

David Brostoff

unread,
Aug 25, 2016, 7:37:32 AM8/25/16
to textwr...@googlegroups.com
On Aug 24, 2016, at 8:40 AM, Christopher Stone <listm...@suddenlink.net> wrote:
>
> You want to limit the number of digits found to 1 and 3 like so:
>
> Find:
>
> \b(\d{1}),(\d{3})\b
>
> Replace:
>
> \1\2
>
> Using the word boundaries (\b) you keep it from finding embedded strings like:
>
> 11,234
>
> You can also use backward and forward looking assertions to find only the comma between the patterns:
>
> Find:
>
> (?<=\b\d),(?=\d{3}\b)
>
> Replace:
>
> Nothing.

Both these solutions do exactly what I wanted. Is there any reason to prefer one or the other?

David

David Brostoff

unread,
Aug 25, 2016, 7:37:34 AM8/25/16
to textwr...@googlegroups.com
On Aug 24, 2016, at 8:40 AM, Christopher Stone <listm...@suddenlink.net> wrote:
>
> That pattern fill find from 1 to 3 digits on each side of the comma, so strings like this will also be found:
>
> 1,1
> 1,12
> 12,1
> 22,22
> 333,333
>
> You want to limit the number of digits found to 1 and 3 like so:
<snip>

Thank you for the great information--it's like taking a tutorial.

David

Christopher Stone

unread,
Aug 25, 2016, 8:47:16 PM8/25/16
to TextWrangler-Talk
On Aug 24, 2016, at 22:34, David Brostoff <dav...@earthlink.net> wrote:
Both these solutions do exactly what I wanted. Is there any reason to prefer one or the other?


Hey David,

I suspect that the first pattern will be a little more efficient (without playing with backtracking), but I'm not certain.

If Ronald Kimball is listening he might have an insight or two.

The nice thing about the second pattern is that it doesn't touch the leading and trailing text anchors – only the comma you want to remove is touched.

--
Best Regards,
Chris

David Brostoff

unread,
Aug 26, 2016, 2:45:20 PM8/26/16
to textwr...@googlegroups.com
Chris,

Thank you--I appreciate your help.

David

> On Aug 25, 2016, at 5:47 PM, Christopher Stone <listm...@suddenlink.net> wrote:
>
> On Aug 24, 2016, at 22:34, David Brostoff <dav...@earthlink.net> wrote:
>> Both these solutions do exactly what I wanted. Is there any reason to prefer one or the other?
>
> Hey David,
>
> I suspect that the first pattern will be a little more efficient (without playing with backtracking), but I'm not certain.
>
> If Ronald Kimball is listening he might have an insight or two.
>
> The nice thing about the second pattern is that it doesn't touch the leading and trailing text anchors – only the comma you want to remove is touched.
>
> --
> Best Regards,
> Chris
>
>
> --
> This is the TextWrangler Talk public discussion group.
> If you have a feature request or would like to report a problem,
> please email "sup...@barebones.com" instead of posting here.
> ---
> You received this message because you are subscribed to the Google Groups "TextWrangler Talk" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to textwrangler...@googlegroups.com.

Reply all
Reply to author
Forward
0 new messages