While trying to solve the following problem, it seems that Go regexp
replacement is missing an important functionality -- able to do
full-fledged replacing with user defined functions.

- I know that "ReplaceAllStringFunc" is designed to do that, however, it is
missing an important aspect of regexp replacement -- the capability of
directly handling matched groups within it.
- I know that "expand" is able to handle matched groups within it, but the
problem is that it is ignoring anything else apart from the replacing part.

So, it seems that Go regexp replacement lacks the ability of direct groups
handling functionality. I know there are ways to workaround it, but the
problem is that they are just workarounds, and, different people
have different workarounds. I.e., it is chaotic without a canonical way of
doing it.

To illustrate, let me use a simpler example than the following. So here is
the input and output that can demonstrate the problem:

INPUT:

We need to increase the precisions. Here is the plan:

- crc8; Crc12

- aes16; Aes32

- CRC32 and then rename it to just Crc


OUTPUT:

We need to increase the precisions. Here is the plan:

- from crc8 to crc12 (8 * 1.5 = 12); from Crc12 to Crc18 (12 * 1.5 = 18)
- from aes16 to aes24 (16 * 1.5 = 24); from Aes32 to Aes48 (32 * 1.5 = 48)
- from CRC32 to CRC48 (32 * 1.5 = 48) and then rename it to just Crc


To get the output with Perl:

perl -pe 's((crc|aes)(\d+))($want=$2; $want+=$2/2; "from $1$2 to $1$want
($2 * 1.5 = $want)")ige'


I.e., Perl can use the matched groups in the user defined replace function
without haggle. Can Go do that *without haggle*?

Thanks

On Sun, Nov 6, 2016 at 5:35 PM, Tong Sun wrote:

>
>
> On Saturday, November 5, 2016 at 3:51:55 PM UTC-4, Tong Sun wrote:
>>
>>
>> On Saturday, November 5, 2016 at 3:42:27 PM UTC-4, Tong Sun wrote:
>>>
>>>
>>> On Sat, Nov 5, 2016 at 12:26 PM, Sam Whited  wrote:
>>>
>>>> On Fri, Nov 4, 2016 at 4:32 PM, Tong Sun wrote:
>>>> > How to beautify a given XML string in GO?...
>>>
>>> I guess such thing doesn't exist, but let me ask away anyway -- the
>>> following is exactly what I was looking for, couldn't express better than
>>> his:
>>>
>>> from http://stackoverflow.com/questions/21117161:
>>>
>>> I like this solution, but am still in search of a Golang XML
>>>> formatter/prettyprinter that doesn't rewrite the document (other than
>>>> formatting whitespace). Marshalling or using the Encoder will change
>>>> namespace declarations. For example an element like "<ns1:Element/>" will
>>>> be translated to something like '<Element xmlns="ns1"></Element>' which
>>>> seems harmless enough except when the intent is to not alter the xml other
>>>> than formatting. – James McGill
>>>> <http://stackoverflow.com/users/4979966/james-mcgill> Nov 12 '15
>>>> <http://stackoverflow.com/questions/21117161/go-how-would-you-pretty-print-prettify-html#comment55137594_27141132>
>>>
>>>
>>> Using Sam's above code as an example,
>>>
>>> https://play.golang.org/p/JUqQY3WpW5
>>>
>>> The above code format the following XML
>>>
>>> <soapenv:Envelope xmlns:soapenv="http://schemas.
>>> xmlsoap.org/soap/envelope/"
>>>   xmlns:ns="http://example.com/ns";>
>>>    <soapenv:Header/>
>>>    <soapenv:Body>
>>>      <ns:request>
>>>       <ns:customer>
>>>        <ns:id>123</ns:id>
>>>        <ns:name type="NCHZ">John Brown</ns:name>
>>>       </ns:customer>
>>>      </ns:request>
>>>    </soapenv:Body>
>>> </soapenv:Envelope>
>>>
>>>
>>> into this:
>>>
>>> <Envelope xmlns="http://schemas.xmlsoap.org/soap/envelope/";
>>> xmlns:_xmlns="xmlns" _xmlns:soapenv="http://schemas
>>> .xmlsoap.org/soap/envelope/" _xmlns:ns="http://example.com/ns";>
>>>  <Header xmlns="http://schemas.xmlsoap.org/soap/envelope/";></Header>
>>>  <Body xmlns="http://schemas.xmlsoap.org/soap/envelope/";>
>>>   <request xmlns="http://example.com/ns";>
>>>    <customer xmlns="http://example.com/ns";>
>>>     <id xmlns="http://example.com/ns";>123</id>
>>>     <name xmlns="http://example.com/ns"; type="NCHZ">John Brown</name>
>>>    </customer>
>>>   </request>
>>>  </Body>
>>> </Envelope>
>>>
>>>
>>> I know they are the same in syntax, however they look totally different.
>>>
>>> Any way (e.g., to tweak encoding/xml) to make the beautified string look
>>> closer to the original?
>>>
>>
>> FTR, trying to look into that direction myself, ..., I *got it working*.
>> After all, XML is much more structured than HTML. Here is what it was
>> previously:
>>
>
> $ echo '<Envelope xmlns=http://schemas.xmlsoap.org/soap/envelope/
> xmlns:_xmlns=xmlns _xmlns:soapenv=http://schemas.
> xmlsoap.org/soap/envelope/ _xmlns:ns=http://example.com/ns><Header xmlns=
> http://schemas.xmlsoap.org/soap/envelope/></Header><Body xmlns=
> http://schemas.xmlsoap.org/soap/envelope/><request xmlns=
> http://example.com/ns><customer xmlns=http://example.com/ns><id xmlns=
> http://example.com/ns>123</id><name xmlns=http://example.com/ns
> type=NCHZ>John Brown</name></customer></request></Body></Envelope>' |
> perl -pe 's/(?<=>)\s+(?=<)//g; s(<(/?)([^/>]+)(/?)>\s*(?=(</?
> ))?)($indent+=$3?0:$1?-1:1;"<$1$2$3>".($1&&($4 eq"</")?"\n".("
>  "x$indent):$4?"\n".("  "x$indent):""))ge'
> <Envelope xmlns=http://schemas.xmlsoap.org/soap/envelope/ xmlns:_xmlns=
> xmlns _xmlns:soapenv=http://schemas.xmlsoap.org/soap/envelope/ _xmlns:ns=
> http://example.com/ns><Header xmlns=http://schemas.xmlsoap.
> org/soap/envelope/></Header>
> <Body xmlns=http://schemas.xmlsoap.org/soap/envelope/><request xmlns=
> http://example.com/ns><customer xmlns=http://example.com/ns><id xmlns=
> http://example.com/ns>123</id>
> <name xmlns=http://example.com/ns type=NCHZ>John Brown</name>
> </customer>
> </request>
> </Body>
> </Envelope>
>
>
>
> After simplified the algorithm, it became the following, and I'm *quite
> happy* with its look. I believe it'd be much faster than any XML
> decode/encode route. *Starting to convert the algorithm into Go code*...
>
> $ echo '<Envelope xmlns=http://schemas.xmlsoap.org/soap/envelope/
> xmlns:_xmlns=xmlns _xmlns:soapenv=http://schemas.
> xmlsoap.org/soap/envelope/ _xmlns:ns=http://example.com/ns><Header xmlns=
> http://schemas.xmlsoap.org/soap/envelope/></Header><Body xmlns=
> http://schemas.xmlsoap.org/soap/envelope/><request xmlns=
> http://example.com/ns><customer xmlns=http://example.com/ns><id xmlns=
> http://example.com/ns>123</id><name xmlns=http://example.com/ns
> type=NCHZ>John Brown</name></customer></request></Body></Envelope>' |
> perl -pe 's/(?<=>)\s+(?=<)//g; s(<(/?)([^>]+)(/?)>)($indent+=
> $3?0:$1?-1:1;"<$1$2$3>"."\n".("  "x$indent))ge'
>
> <Envelope xmlns=http://schemas.xmlsoap.org/soap/envelope/ xmlns:_xmlns=
> xmlns _xmlns:soapenv=http://schemas.xmlsoap.org/soap/envelope/ _xmlns:ns=
> http://example.com/ns>
>   <Header xmlns=http://schemas.xmlsoap.org/soap/envelope/>
>     </Header>
>   <Body xmlns=http://schemas.xmlsoap.org/soap/envelope/>
>     <request xmlns=http://example.com/ns>
>       <customer xmlns=http://example.com/ns>
>         <id xmlns=http://example.com/ns>
>           123</id>
>         <name xmlns=http://example.com/ns type=NCHZ>
>           John Brown</name>
>         </customer>
>       </request>
>     </Body>
>   </Envelope>
>
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to