I want to extract a string from a piece of text. This string must start end end with a certain string.
Example:
Word 1 = "Hello"
Word 2 = "World"
Text:
Hello, this is a sentence.
The whole World can read this.
What World?
The piece of text i want to extract is:
Hello, this is a sentence.
The whole World
What kind of regular exception should i use for extraction of the string.
Note: the string 'World' occurs twice.
Thanks
^\bHello\b.*?\bWorld\b
Where the "." also matches newline! Note the word boundaries \b, you don't want to match anything which is not exactly Hello or World, as if those words were part of other words.
if ($subject =~ m/^\bHello\b.*?\bWorld\b/s) {
$result = $&;
}
Note the s modified which instructs
.
to match newline characters too.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With