Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #38105

Re: regex replaceAll?

From Kiwi User <martin@mydomain.invalid>
Newsgroups comp.lang.java.programmer
Subject Re: regex replaceAll?
Date 2018-03-02 01:33 +0000
Organization albasani.net
Message-ID <p7a9ke$tal$2@news.albasani.net> (permalink)
References (3 earlier) <slrnp9gtn0.5mg.avl@logic.at> <8c677118-8115-409d-8d3b-396e54b063ae@googlegroups.com> <slrnp9gvpc.5mg.avl@logic.at> <p7a6n9$tal$1@news.albasani.net> <p7a72k$qqd$1@gioia.aioe.org>

Show all headers | View raw


On Thu, 01 Mar 2018 19:49:26 -0500, Arne Vajhøj wrote:

> On 3/1/2018 7:43 PM, Kiwi User wrote:
>> On Thu, 01 Mar 2018 22:31:08 +0000, Andreas Leitgeb wrote:
>>> It's pretty normal that some attempt at writing an RE to solve a
>>> problem doesn't work at first time.
>>>
>>> Then, there's essentially two strategies:
>>>
>>>   1) randomly shuffle around the funny symbols and see if that
>>>        works then.  If the reshuffled variant doesn't work, either, go
>>>        back to original and defend it: "tried other way, but didn't
>>>        work any better."
>>>
>>>   2) analyze what the RE is really doing, and refine it to do
>>>        what you want it to do.
>>>
>>> take your choice...
>>>
>> Another way to test regexes may help: use an online regex test tool
>> like this one:
>>   
>> http://www.regexplanet.com/advanced/java/index.html
>> 
>> It helps because it:
>> - tests an expression without you writing anything except the regex -
>> lets you submit a set of test strings and run repeated tests against
>>    them
>> - has check boxes for making caseless comparisons, etc.
> 
> Yes.
> 
> But if the regex is so complicated/unreadable that it is not obvious
> whether it works or not, then I would question whether regex is the
> right choice.
>
Yes - but surely that should go without saying. Choosing the right tool 
for a job is an essential part of the programmer's trade.

The point I was trying to make wasn't specifically about the regex in 
question, but that there are often more convenient ways of testing and/or 
developing regexes than trying to do it in situ.

For instance, I regularly develop my own rules for Spamassassin, which is 
written in Perl and uses Perl regexes as an essential part of its spam 
recognition rules. grep, a commonly used file scanning tool in Linux and 
other Unices uses regexes to select which input lines should be output. 
Its -P option tells it to use the Perl regex dialect rather than its own, 
making it very easy to use when developing a Perl regex. Just create a 
file containing example strings arranged one per line and with each line 
annotated to show whether the string should match or not. Then run the 
regex against the file:

  grep -P 'perlRegex' testfile.txt

to show matching lines and

  grep -PV 'perlRegex' testfile.txt

to show non-matching lines. In these examples I quotes the regexes  
to stop the command shell from modifying the regex.

Since there's no equivalent way of testing Java regexes, there's no 
reason why you shouldn't write a simple Java command line program that 
would do exactly the same: accepting the regex as a parameter, applying 
it to every line in a test file and displaying the matches. 

However, given that the Java regex test page is there to be used, why not 
use it?


-- 
Martin    | martin at
Gregorie  | gregorie dot org

Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

regex replaceAll? Eric Douglas <e.d.programmer@gmail.com> - 2018-03-01 06:19 -0800
  Re: regex replaceAll? Andreas Leitgeb <avl@logic.at> - 2018-03-01 19:03 +0000
    Re: regex replaceAll? Eric Douglas <e.d.programmer@gmail.com> - 2018-03-01 12:15 -0800
      Re: regex replaceAll? Daniele Futtorovic <da.futt.news@laposte.net> - 2018-03-01 21:51 +0100
        Re: regex replaceAll? Eric Douglas <e.d.programmer@gmail.com> - 2018-03-01 13:36 -0800
          Re: regex replaceAll? Arne Vajhøj <arne@vajhoej.dk> - 2018-03-01 16:59 -0500
      Re: regex replaceAll? Andreas Leitgeb <avl@logic.at> - 2018-03-01 21:55 +0000
        Re: regex replaceAll? Eric Douglas <e.d.programmer@gmail.com> - 2018-03-01 14:00 -0800
          Re: regex replaceAll? Andreas Leitgeb <avl@logic.at> - 2018-03-01 22:31 +0000
            Re: regex replaceAll? Arne Vajhøj <arne@vajhoej.dk> - 2018-03-01 17:52 -0500
              Re: regex replaceAll? Andreas Leitgeb <avl@logic.at> - 2018-03-02 00:14 +0000
            Re: regex replaceAll? Kiwi User <martin@mydomain.invalid> - 2018-03-02 00:43 +0000
              Re: regex replaceAll? Arne Vajhøj <arne@vajhoej.dk> - 2018-03-01 19:49 -0500
                Re: regex replaceAll? Kiwi User <martin@mydomain.invalid> - 2018-03-02 01:33 +0000
                Re: regex replaceAll? Arne Vajhøj <arne@vajhoej.dk> - 2018-03-02 13:00 -0500
                Re: regex replaceAll? Kiwi User <martin@mydomain.invalid> - 2018-03-03 20:07 +0000
            Re: regex replaceAll? Eric Douglas <e.d.programmer@gmail.com> - 2018-03-02 05:40 -0800
              Re: regex replaceAll? Andreas Leitgeb <avl@logic.at> - 2018-03-02 17:46 +0000
                Re: regex replaceAll? Eric Douglas <e.d.programmer@gmail.com> - 2018-03-02 10:38 -0800
                Re: regex replaceAll? Andreas Leitgeb <avl@logic.at> - 2018-03-02 20:15 +0000
                Re: regex replaceAll? Eric Douglas <e.d.programmer@gmail.com> - 2018-03-02 12:43 -0800
                Re: regex replaceAll? Eric Douglas <e.d.programmer@gmail.com> - 2018-03-02 12:58 -0800
                Re: regex replaceAll? Daniele Futtorovic <da.futt.news@laposte.net> - 2018-03-03 00:06 +0100
  Re: regex replaceAll? Daniele Futtorovic <da.futt.news@laposte.net> - 2018-03-01 20:29 +0100
    Re: regex replaceAll? Daniele Futtorovic <da.futt.news@laposte.net> - 2018-03-01 20:30 +0100
    Re: regex replaceAll? Eric Douglas <e.d.programmer@gmail.com> - 2018-03-01 12:28 -0800
      Re: regex replaceAll? Daniele Futtorovic <da.futt.news@laposte.net> - 2018-03-01 21:40 +0100
    Re: regex replaceAll? Eric Douglas <e.d.programmer@gmail.com> - 2018-03-01 13:52 -0800
      Re: regex replaceAll? Daniele Futtorovic <da.futt.news@laposte.net> - 2018-03-02 02:25 +0100

csiph-web