smiikr smiikr - 7 months ago 9
Perl Question

Can perl regex only deal with limited text?

I have been trying to match a string and output all the text after the match.

e.g: I attempt to print everything after the word "hello"

$string = "hello its a good day"

output: its a good day.


the following code has worked very efficiently:

my ($extract) = $string =~ /hello\s*(.+)$/;


The issue comes when I try use a large amount of text such as when the string is a file with hundreds of words (like a pragraph with multiple lines) and I want to only use the text after a certain word just like above. It just doesnt respond at all.

example

Dear sally, how is your day, I heard things are getting better
etc etc

how is everyone

sometimes. not alwyas etc etc etc etc

kind regards


So then I want to capture everything after "Dear sally".

the output would be:

, how is your day, I heard things are getting better
etc etc

how is everyone

sometimes. not alwyas etc etc etc etc

kind regards


I tried to use the split command instead but it just outputs numbers.

Answer

"It just doesnt respond at all" isn't very clear. A dot . in a regex pattern will ordinarily match any character except for a newline

Probably your input data is spread across multiple lines?

You can use the /s modifier to alter dot to match anything at all, but in that circumstance a pattern like /hello\s*(.+)$/s will match all of the rest of the file after hello

You should explain what you're trying to achieve; then we could help better