Due to rxapply(and rxfrom) bug that I posted a minute ago, you have to
currently do the following when the replacement pattern's length is 1:

   ('AND';,:'&') pysub 'alpha AND bravo'

alpha & bravo


Or, you may switch the definition of rxfrom in regex.ijs to the
commented-out one.


On Sun, Aug 31, 2014 at 12:35 AM, June Kim (김창준) <[email protected]> wrote:

> Hi
>
> Is this what you want? (Note: \n is a backreference to n-th match of the
> pattern)
>
> assert '**123*'-:('[0-9]+';'**\0*') pysub '123'
>
> assert 'ab 123*y*45 de'-:('([0-9]+)([x]+)([0-9]+)';'\1*y*\3') pysub 'ab
> 123x45 de'
>
> assert '
> https://www.jsoftware.com/wiki/help/pcre/pcrepattern.html'-:('http://([
> ^/]+)';'https://\1/wiki') pysub '
> http://www.jsoftware.com/help/pcre/pcrepattern.html'
>
>
> Following is my quick and crude implementation(written and tested in J801):
>
>
> NB. API is inspired by python's re.sub :
> https://docs.python.org/2/library/re.html
>
>
> coclass 'PySub'
>
> load 'regex'
>
>
> create=: 3 : 0
>
> pysub_y=: 0 $ 0
>
> pysub_matches=: 0 $ 0
>
> )
>
>
> destory=: codestroy
>
>
> backref=: 3 : 0
>
> ind=.". }. y
>
> {.>(ind {"2 pysub_matches) rxfrom pysub_y
>
> )
>
>
> pysub=: 4 : 0
>
> 'pat repl'=.x
>
> pysub_y=:y
>
> pysub_matches=:pat rxmatches y
>
> rptxt=.'\\[0-9]+' backref rxapply repl
>
> (pat;rptxt) rxrplc y
>
> )
>
>
>
> pysub_z_=: 4 : 0
>
> obj=. '' conew 'PySub'
>
> r=.x pysub__obj y
>
> destory__obj ''
>
> r
>
> )
>
>
> assert '**123*'-:('[0-9]+';'**\0*') pysub '123'
>
> assert 'ab 123*y*45 de'-:('([0-9]+)([x]+)([0-9]+)';'\1*y*\3') pysub 'ab
> 123x45 de'
>
> assert '
> https://www.jsoftware.com/wiki/help/pcre/pcrepattern.html'-:('http://([
> ^/]+)';'https://\1/wiki') pysub '
> http://www.jsoftware.com/help/pcre/pcrepattern.html'
>
>
> On Sat, Aug 30, 2014 at 5:38 PM, Raul Miller <[email protected]>
> wrote:
>
>> Actually, that's buggy.
>>
>> I should have said:
>>
>>    mid=. (,:({:-{.@,,0:)M) rxcut 1{::segs
>>
>> Oops...
>>
>> --
>> Raul
>>
>>
>> On Sat, Aug 30, 2014 at 4:13 AM, Raul Miller <[email protected]>
>> wrote:
>> > J has "PCRE" regular expressions:
>> >
>> >    require 'regex'
>> >    url=: 'http://www.jsoftware.com/help/pcre/pcrepattern.html'
>> >    >('http://[^/]+'&rxmatch rxcut ]) url
>> >
>> > http://www.jsoftware.com
>> > /help/pcre/pcrepattern.html
>> >
>> > Now.. regular expressions seem like they ought to be a natural fit for
>> > J - if nothing else, the sort of people who turn up their nose at J
>> > syntax are probably either (a) so inured to regular expressions that
>> > they forget how they look, or (b) can't stand regular expressions.
>> >
>> > That said... PCRE stands for "perl compatible regular expressions" and
>> > J is missing a significant feature from perl's implementation: string
>> > interpolation.
>> >
>> > Perl has a string language where you can specify variables to plug
>> > into the string, and PCRE ties into that mechanism. J is significantly
>> > more verbose for this kind of thing.
>> >
>> > In other words, in perl, I could do this:
>> >   $url=~ s{http://([^/]+)}{https://$1/wiki};
>> >
>> > and if $url were the value of url, above (in perl scalar variable
>> > names begin with $ and strings are treated as scalars - perl does not
>> > have J's concept of rank and instead approximates it with the rank
>> > indicator hard-coded into the variable name), then the above sentence
>> > would modify its value to be:
>> >
>> > https://www.jsoftware.com/wiki/help/pcre/pcrepattern.html
>> >
>> > Here's some equivalent (explicit) J:
>> >
>> >   if. {:,M=. 'http://([^/]+)' rxmatch url do.
>> >     segs=. M rxcut url
>> >     mid=. (,:({:-{.@{.)M) rxcut 1{::segs
>> >     url=. ;}.(<'https://',(1{::mid),'/wiki') 1} segs
>> >   end.
>> >
>> > As you can see, it's a bit more complex.
>> >
>> > So ...
>> >
>> > I'm not going to tackle writing such a thing - at least, not tonight.
>> > But that's something that might be nice - support for a string
>> > interpolation language, including tie-in support for the jregex
>> > locale.
>> >
>> > (I am mostly writing this to organize my thoughts - as you can see,
>> > this topic is messy enough in J that organizing thoughts is probably a
>> > good idea.)
>> >
>> > (And, yes, this particular task could be accomplished more easily in J
>> > - my actual task is more complicated and needs a more involved regular
>> > expression. I'm just outlining what I see as an issue.)
>> >
>> > Thanks,
>> >
>> > --
>> > Raul
>> ----------------------------------------------------------------------
>> For information about J forums see http://www.jsoftware.com/forums.htm
>>
>
>
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Reply via email to