How to have \D ignore newline character?

58 views
Skip to first unread message

Tim A

unread,
Dec 12, 2021, 12:21:10 PM12/12/21
to BBEdit Talk
Simple task to strip out all non-digits in telephone numbers. \D works but also matches newline at the end of every line. Is there a way to 'turn off' the newline match?

I can do a 
Find: [-() ]   which will match the usual possible non-digit characters which I can replace with nothing, but figure I am missing something simple and worth knowing here.

Thanks.

jj

unread,
Dec 12, 2021, 12:45:22 PM12/12/21
to BBEdit Talk
Hi Tim,

Try using a negative character class like [^\d\n\r] instead of \D.

HTH

Jean Jourdain

Tim A

unread,
Dec 13, 2021, 8:41:06 AM12/13/21
to BBEdit Talk
Try using a negative character class like [^\d\n\r] 
That will do it! .. and as an extra benefit catches any periods used as a delimiter as well.

Deepening the challenge, the phone numbers are actually in the second of three columns separated by tabs. If I can get the phone numbers stripped I can then impose a uniform format on them. But how do I focus the find/replace on just this second column? I can do this in many small steps, but figure there must be an elegant way to do it in a single step.

Before...

Name1        1234567890        Single Lifetime
Name2        123-4567890        Joint Lifetime
Name3        123-456-7890        Joint Lifetime
Name4        (123)4567890        Single
Name5        (123) 4567890        Single
Name6        (123)-4567890        Single
Name7        (123)456-7890        Joint
Name8        123.456.7890        Joint

After...
Name1        1234567890        SingleLifetime
Name2        1234567890        JointLifetime
Name3        1234567890        JointLifetime
Name4        1234567890        Single
Name5        1234567890        Single
Name6        1234567890        Single
Name7        1234567890        Joint
Name8        1234567890        Joint






ThePorgie

unread,
Dec 13, 2021, 9:36:13 AM12/13/21
to BBEdit Talk
Pulling your text and replacing the spaces in the tab areas and then Running the following
(?:[- (\.\d]+)?(\d{3})(?:[- )\.]+)?(\d{3})(?:[- )\.]+)?(\d{4})
with a replacement of
\1-\2-\3
yields the following
Name1        123-456-7890        Single Lifetime
Name2        123-456-7890        Joint Lifetime
Name3        123-456-7890        Joint Lifetime
Name4        123-456-7890        Single
Name5        123-456-7890        Single
Name6        123-456-7890        Single
Name7        123-456-7890        Joint
Name8        123-456-7890        Joint

Is that what your looking for?

ThePorgie

unread,
Dec 13, 2021, 9:40:08 AM12/13/21
to BBEdit Talk

"...replacing the spaces in the tab areas..." Should of added "to simulate the text you describe."

Tim A

unread,
Dec 13, 2021, 1:37:34 PM12/13/21
to BBEdit Talk
On Monday, December 13, 2021 at 6:36:13 AM UTC-8 ThePorgie wrote:
Pulling your text and replacing the spaces in the tab areas and then Running the following
(?:[- (\.\d]+)?(\d{3})(?:[- )\.]+)?(\d{3})(?:[- )\.]+)?(\d{4})
with a replacement of
\1-\2-\3              Is that what your looking for?
 
Ah, a non capture of a possible crud character then capturing the three numeric groups.
Finally hyphenating the result.
Thanks.

Christopher Stone

unread,
Dec 13, 2021, 2:19:51 PM12/13/21
to BBEdit-Talk
On Dec 12, 2021, at 23:38, Tim A <timaa...@gmail.com> wrote:
Deepening the challenge, the phone numbers are actually in the second of three columns separated by tabs. If I can get the phone numbers stripped I can then impose a uniform format on them.


Hey Tim,

Find:

[(]?(\d{3})\D*(\d{3})\D*(\d{4})

Replace:

\1-\2-\3


--
Best Regards,
Chris

ThePorgie

unread,
Dec 13, 2021, 2:45:22 PM12/13/21
to BBEdit Talk
Much prettier & easier to read sir....Well played.

Tim A

unread,
Dec 13, 2021, 3:13:27 PM12/13/21
to BBEdit Talk
Learning that regular expressions is often about focussing on key trees in the forest.
This is a great way to learn. Thanks all.
Reply all
Reply to author
Forward
0 new messages