Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hello PerlMonks who are always smarter than me. I'm trying to write a regex to match "---" followed by a new line to parse a number of files and return some data with no success. Here's the code that I have that's not working , any assistance is greatly appreciated.

@matches7 = ($text =~ /(.{0,0})\K(---\s*)(.{0,200})/gisx);

Replies are listed 'Best First'.
Re: regex to match "---" followed by new line
by haukex (Archbishop) on Jun 29, 2017 at 07:27 UTC

    Your regex does seem to do something for me... but what does your $text contain? If you're reading it from a file, are you reading the file line-by-line or are you slurping it all at once? There are several things I could point out about the regex, like how I'm not sure what (.{0,0})\K is supposed to be doing, or that the dots . will match anything, including newlines (because of /s) and dashes. But reviewing the regex doesn't make much sense without knowing what it's supposed to match against - please provide an SSCCE with several examples of what should match and what shouldn't!

    use Data::Dump; dd "---\nFoo\n---\nBar\n" =~ /(.{0,0})\K(---\s*)(.{0,200})/gisx; __END__ ("", "---\n", "Foo\n---\nBar\n")

    Also, note that you could set the input record separator $/ to something like "---\n" to read a file in chunks separated by that string.

Re: regex to match "---" followed by new line
by 1nickt (Canon) on Jun 29, 2017 at 08:07 UTC

    Hi, you're not really using a regexp to parse a YAML document, are you?


    The way forward always starts with a minimal test.
    A reply falls below the community's threshold of quality. You may see it by logging in.