Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

comment on

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

Hello,

I am a beginner trying to use a regular expression to search for a pattern. The problem is that the pattern is split across multiple lines. For example, if I am searching for 'abc' in a file containing the following:


abcdefab
cdefa
bcdef

I should get 3 matches.

The first thing I tried was to write a While script that reads the file line by line, chomps the line and stores everything in a single variable which I then search. This works well when I use a small sample file but for some reason doesnt work with my actual txt file which is several hundred mb so maybe it is not the most efficient way of doing this.

This is my code  while ($line=<inputfile>){chomp $line; $string=$string.$line;}

A second option according to google is to use /m or /s modifiers. But the problem is that I dont know where the text would be broken by the newline so should I put a . after every character in my regular expression? My actual expression is pretty long so I dont know if that would be the best way to do it.

My regular expression is to search for a keyword and capture the 5 characters before and after it with the keyword also containing a random 10 character sequence in it. $string =~ /(?<=(.....))abc(.{10})def(?=(.....))/g

I am assuming that there is something obvious I am missing here so would appreciate any help.

Thanks

In reply to Regular expressions across multiple lines by abcd

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 making s'mores by the fire in the courtyard of the Monastery: (4)
As of 2024-04-25 07:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found