Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

regex search

by zuma53 (Beadle)
on Jun 19, 2012 at 15:07 UTC ( [id://977094]=perlquestion: print w/replies, xml ) Need Help??

zuma53 has asked for the wisdom of the Perl Monks concerning the following question:

This is probably a really silly question, but I am stumped! Let's say I have these strings:
$A:  abc.def.ghi
$B:  abc
To ask "does $A start with $B" is trivial:
$A =~ /^${B}/;
But what is the complement of this? That is, "is $B a prefix of $A?"
$B =~ /?$A?/;
Here, I am taking the fact that $B is shorter than $A, but this logic fails when $B is longer than $A. I can probably do some clunky len/substring preprocessing, but I am certain there's a more elegant regex way (isn't there always?)

Thanks in advance.

J

Replies are listed 'Best First'.
Re: regex search
by AnomalousMonk (Archbishop) on Jun 19, 2012 at 16:59 UTC

    I, too, am confused by the whole question of the 'complimentary' (or complementary, or converse) question. Maybe the question can be usefully (and symmetrically) reframed as "Are there any characters in common at the beginnings of both strings?". This could be answered by index as well as by a regex.

    >perl -wMstrict -le "my @set = ('abc.def.ghi', 'abc', '...'); ;; for my $p (@set) { for my $q (@set) { print qq{'$p', '$q' common at start? }, $q =~ m{ \A \Q$p\E }xms || $p =~ m{ \A \Q$q\E }xms ? 'yes' : 'no'; } } ;; print ''; for my $p (@set) { for my $q (@set) { print qq{'$p', '$q' common at start? }, 0 == index($p, $q) || 0 == index($q, $p) ? 'yes' : 'no'; } } " 'abc.def.ghi', 'abc.def.ghi' common at start? yes 'abc.def.ghi', 'abc' common at start? yes 'abc.def.ghi', '...' common at start? no 'abc', 'abc.def.ghi' common at start? yes 'abc', 'abc' common at start? yes 'abc', '...' common at start? no '...', 'abc.def.ghi' common at start? no '...', 'abc' common at start? no '...', '...' common at start? yes 'abc.def.ghi', 'abc.def.ghi' common at start? yes 'abc.def.ghi', 'abc' common at start? yes 'abc.def.ghi', '...' common at start? no 'abc', 'abc.def.ghi' common at start? yes 'abc', 'abc' common at start? yes 'abc', '...' common at start? no '...', 'abc.def.ghi' common at start? no '...', 'abc' common at start? no '...', '...' common at start? yes
Re: regex search
by aaron_baugher (Curate) on Jun 19, 2012 at 20:25 UTC
    To ask "does $A start with $B" is trivial:

    $A =~ /^${B}/;

    But what is the complement of this? That is, "is $B a prefix of $A?"

    In my understanding of English, those don't complement each other; they mean the same thing. A "prefix" is a group of letters at the beginning of a word, so if $B is a prefix of $A, then $A starts with $B. Your line of code above tests for that situation, whichever you call it.

    Aaron B.
    Available for small or large Perl jobs; see my home node.

Re: regex search
by cheekuperl (Monk) on Jun 19, 2012 at 15:29 UTC
    With "compliment" you probably mean searching for $A at the beginning of $B- which essentially fails since $A is longer than $B!
      The compliment (that I am looking for) would be that $B is a prefix of $A.

      J

        But your first regex is checking that.

Re: regex search
by muba (Priest) on Jun 19, 2012 at 19:10 UTC

    Given that AnomalousMonk's response above doesn't answer your question, I think I get you now. What you're trying to achieve, actually, is this: you have a situation, in which you have these set of strings:

    my $superstring = "123abc.def.ghi"; my $substring = "abc.def.ghi"; my $prefix = "123";

    And what you want to do is to see whether $substring appears in $superstring, but only if it's preceded by $prefix?

    Like this?

    #### (pfff where is my head at. This won't work of course) $superstrin +g =~ m/$B$A/ $superstring =~ m/$prefix$superstring/;

    If that's not it, then you'll have to explain what you mean with 'prefix' in this context.

    Edit:Caught a stupid brainfail in the regex code. Fixed.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://977094]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (2)
As of 2024-04-20 05:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found