Re: all matches of a regex-continued

From: Qznur__ta=Fetan?= (oznurtastan_at_su.sabanciuniv.edu)
Date: 02/20/04


To: "Perl Lists" <beginners@perl.org>
Date: Fri, 20 Feb 2004 10:54:45 +0200

I am still dealing with the same problem.
Rob has suggested me a good solution for macthing consecutive patterns like
 H K D but not more looser ones like for K[ED]{3,5}? L.{3}A
andn my poor perl knowledge doesn't help me to generalize it: /

In the below link I came across

http://www.perl.com/pub/a/2002/06/04/apo5.html?page=8

    $_ = "abracadabra";
        @all = m:any /a.*?a/;
produces:

    abra abraca abracada abracadabra aca acada acadabra ada adabra abra
 Is there a version available that supports this structure?
Or are there any creative ideas of Perl wizards?
Thanks in advance
cheers
oznur
Öznur Tastan wrote:
>
> Your suggestion was quite helpful but I got stuck when I try to modify it for
> general purpose. May be you will have an idea and want to help.
>
> The foreach $n( 1...length sequence) solves the combinatorial problem but the
> combinations can happen in second or third cases
>
> so I need to repeat the foreach loop
> foreach $m(1..length $sequence){
> foreach $n(1..length sequence){
> foreach $f(1..length sequence){
> foreach $r(1..length sequence){
> next unless $sequence =~ /> (.{$m})H(,{$n})K(.{$f})D(.{$R})/;
>
> But the string was an example an in general case there can be n letters of just
> like H K and D so I need n+1 for each loops repeated and I don't have any idea
> how to write a foreach loop that can should be repeated n times.

Hi Öznur.

Yes, I did suspect my previous answer wouldn't handle the general case, but
I hoped it may be good enough. I'm sure it's not possible using a simple regex.

I've written subroutine split_list() below, which takes a string of characters to
split on and a string to split, a little like the split() built-in. It finds all
the ways to split the string at each of the characters in sequence.

It's a recursive subroutine to make it neater. It works by finding a way to
split on the first character in the list and then calling itself to split
the right-hand half on the remaining characters.

The return value is an array of all the possibilities with the split
characters replaced with hyphens.

Post to the list if you need any help with any part of it.

(Thanks, this was an interesting little problem!)

Cheers,

Rob

use strict;
use warnings;

sub split_list {

  my ($list, $string) = @_;

  return ($string) unless $list =~ s/(.)//;
  my $split = $1;

  my @splits;

  while ( $string =~ /$split/g ) {

    my $pos = pos $string;
    my $left = substr $string, 0, $pos - 1;
    my $right = substr $string, $pos;

    push @splits, map "$left-$_", split_list($list, $right);
  }

  return @splits;
}

my @ret = split_list ('HKD', 'xHxxHyyKzDt');

print map "$_\n", @ret;

**OUTPUT

x-xxHyy-z-t
xHxx-yy-z-t

-- 
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Relevant Pages

  • Re: BASIC & Long Strings
    ... If the path's longer than 255 characters, ... parts of RISC OS are going to have trouble dealing with it too. ... Second suggestion; use blocks of memory to store your strings in, ... BASIC's built-in string handling. ...
    (comp.sys.acorn.programmer)
  • Re: Calculating values of a 2d array by comparison of 2 strings
    ... amino acid residues (long sequences of any of 20 characters) these ... If you do need the array form you can generate it from the count hash. ... Thank you very much for the suggestion... ... string of the characters where each character's position in the string is ...
    (comp.lang.perl.misc)
  • Re: How to Delete Blank Spaces in a Field?
    ... Rob, worked perfectly. ... Try the function called LTrim(). ... Just read the field into a string variable, ... Field is 8 characters in length and is ...
    (microsoft.public.access.tablesdbdesign)
  • Re: seach for character
    ... bearing the following fruit: ... all I have to do is to type in a string of characters in one text box. ... do anyone have an idea or suggestion I would gladly like ...
    (microsoft.public.vb.syntax)
  • Re: seach for character
    ... string of characters should appear backwards, ... do anyone have an idea or suggestion I would gladly like ...
    (microsoft.public.vb.syntax)