Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

Dear Monks,

I have a problem I am trying to solve with regular expressions. I want to return all locations of an array of patterns inside a string. I also only want the patterns to match at locations which are a multiple of 3. For example, so far I have the following.

my $string="AAABBBCCCCDDDEEEFFFGGGHHHIII"; my @patterns=('BBB','DDD'); my @index; foreach(@patterns){ while($string=~m/([A-Z]{3})+?$_/g){ push(@index,$-[2]); pos($string)=$-[2]; }

The problem with this is that it matches the first pattern correctly, but it will also match 'CCCDDD' in the second loop because I cannot/dont know how to anchor the pattern to start at a specific location.

To be clear, I only want the patterns in the array to match if there are a number of characters evenly divisible by three before it.

Is there a good way to do this?

Thanks

In reply to Perl Regex Repeating Patterns by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

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

    No recent polls found