Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re: REgular expression to check the string that allows "a","b" and "c" to occur only once in any order.

by inman (Curate)
on Dec 11, 2007 at 12:35 UTC ( #656386=note: print w/replies, xml ) Need Help??


in reply to REgular expression to check the string that allows "a","b" and "c" to occur only once in any order.

I'm not sure whether this is in the rules but it does work. The code below sorts the data before matching. This makes the match fairly trivial since you need to look for the string abc without a an extra "a" or "c".
#! /usr/bin/perl -w use strict; my @testdata = qw (abc cba cab bac bba cbc bbc abcdefg 0a2c3bc); foreach (@testdata) { my $test = join '', sort split //, lc $_; $test =~ /[^a]*abc(?!c)[^c]*/ ? print "Matched $_\n" : print "Didn +'t match $_\n"; }
  • Comment on Re: REgular expression to check the string that allows "a","b" and "c" to occur only once in any order.
  • Download Code

Replies are listed 'Best First'.
Re^2: REgular expression to check the string that allows "a","b" and "c" to occur only once in any order.
by ww (Bishop) on Dec 11, 2007 at 13:31 UTC
    Interesting approach for which ++.

    .oO, however, that this allows cases such as when @testdata contains aabc. IMO that's NOT quite1 (strictly) prohibited by the (very loose) specs from OP:

    Can anybody help to have a regular expression that checks that a,b and c occur in a string in any order.
    SO, the valid strings are abc, bca, cab, cba, bac, acb...
    Invalid strings are aab, abbc, acc etc.

    BUT the presence of abbc in the list of invalid strings suggests that isha may want to requires that each permitted letter occur only once. (Others may well infer differently. As noted, I consider the spec to be imprecise... as is, ISTM, the general case when specs are written by example.)

    1. because, in this case, while the initial "a" is doubled the $test does include an instance of each acceptable character; in other words, ISTM that aabc is NOT - strictly - prohibited in the sample of invalid strings, aab because aab can be read as prohibiting only an incomplete set of the chars a, b and c, while the invalid string abbc does not strictly prohibit having the lead character doubled. Am I being pedantic or fuzzy-brained?

    I do find my own observations to fall short of precision+clarity. Maybe it's too early in the AM... :-)

    #! /usr/bin/perl -w use strict; my @testdata = qw (abc aabc cba cab bac bba cbc bbc abcdefg 0a2c3bc); foreach (@testdata) { my $test = join '', sort split //, lc $_; $test =~ /[^a]*abc(?!c)[^c]*/ ? print "Matched $_\n" : print "Didn +'t match $_\n";

    Output:
    Matched abc
    Matched aabc     <=
    Matched cba
    ....

    Update: added the missing close-paren at the end of para 3

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://656386]
help
Chatterbox?
[Corion]: Discipulus: If you want to rewrite the SVG, then yes, something like XML::Twig should be enough. I would try to not modify the SVG but only modify the display, but so far I haven't convinced IE to change the style...
Corion gives up
[Discipulus]: ah! so you mean if i need a static image i can use Twig, if on the fly better css selector and mody attrs (jquery?), rigth?
[LanX]: not sure what you guys are taking about, but a DOM manipulation in JS shouldn't be difficult
[Corion]: Discipulus: Yeah, that's what I would try (but so far, I can't even change the colors manually in the SVG, which is why I gave up ;) )
Discipulus bad sign..
[Discipulus]: italian doc says to modify somehow the initial SVG before using it with D3.js
[Discipulus]: LanX the fact is that my js ability is even poorer than my Perl one..

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (13)
As of 2017-03-23 09:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Should Pluto Get Its Planethood Back?



    Results (285 votes). Check out past polls.