A Question Derived from Converting decimal value

22 views
Skip to first unread message

ThePorgie

unread,
Mar 16, 2022, 2:06:14 PM3/16/22
to BBEdit Talk
I was tinkering with with Patrick's question regarding replacing the digits where the "n.1" is replaced with "n.33" and "n.2" is replaced with "n.67"
His sample data:
Sample      Desired output
0.2                    0.67
1.1                    1.33
1.1                    1.33
1.2                    1.67
1.1                    1.33
6                        6
3.2                     3.67
8                        8
6.2                     6.67

So I was looking at this and knew it would have to be several passes, but my pattern I was working with for the first pass was "(?P<foo>\d+\.)1"
My question is since I can't use a replace pattern \1 follow by 33 I was using a named pattern, but (?P=foo) doesn't call the named pattern in the replace area of a grep search? Just curious as I couldn't find anything regarding this in the manual.

Patrick Woolsey

unread,
Mar 16, 2022, 4:13:30 PM3/16/22
to bbe...@googlegroups.com
On 3/16/22 at 2:06 PM, thep...@gmail.com (ThePorgie) wrote:

>I was tinkering with with Patrick's question

Not actually my question ;-) though close enough for the matters
at hand.


>So I was looking at this and knew it would have to be several
>passes, but my pattern I was working with for the first pass
>was "(?P<foo>\d+\.)1"
>
>My question is since I can't use a replace pattern \1 follow by 33

You can in fact do this by prefixing a zero "0" to the singular
backreference number, so:

Replace: \0133

will give you the first backreference \01 followed by the string "33".


>I was using a named pattern, but (?P=foo) doesn't call the named pattern
>in the replace area of a grep search? Just curious as I couldn't find
>anything regarding this in the manual.

Please see the section titled "Subpatterns Make Replacement
Powerful" in Chapter 8 (page 202) of the current manual:

Pattern Inserts
=====================================================
[...]

\P<NAME> the text matched by the subpattern NAME


Regards,

Patrick Woolsey
==
Bare Bones Software, Inc. <https://www.barebones.com/>

ThePorgie

unread,
Mar 16, 2022, 7:24:42 PM3/16/22
to BBEdit Talk
Thank you sir! Don't know how I missed that in the manual and I promise I did look there first.
Reply all
Reply to author
Forward
0 new messages