Raku Regex to capture and modify the LFM code blocks

Lars Malmsteen

Update: Corrected code added below

I have a Leanpub flavored markdown* file named sample.md I'd like to convert its code blocks into Github flavored markdown style using Raku Regex

Here's a sample **ruby** code, which
prints the elements of an array:

{:lang="ruby"}
    ['Ian','Rich','Jon'].each {|x| puts x}

Here's a sample **shell** code, which
removes the ending commas and
finds all folders in the current path:

{:lang="shell"}
    sed s/,$//g
    find . -type d

In order to capture the lang value, e.g. ruby from the {:lang="ruby"} and convert it into

```ruby

I use this code

my @in="sample.md".IO.lines;
my @out;
for @in.kv -> $key,$val {
    if $val.starts-with("\{:lang") {
       if $val ~~ /^{:lang="([a-z]+)"}$/ { # capture lang
           @out[$key]="```$0"; # convert it into ```ruby
           $key++;
           while @in[$key].starts-with("    ") {
                 @out[$key]=@in[$key].trim-leading;
                 $key++;
           }
           @out[$key]="```";
       }
    }
    @out[$key]=$val;
}

The line containing the Regex gives Cannot modify an immutable Pair (lang => True) error.

I've just started out using Regexes. Instead of ([a-z]+) I've tried (\w) and it gave the Unrecognized backslash sequence: '\w' error, among other things.

How to correctly capture and modify the lang value using Regex?

  • the LFM format just estimated

Corrected code:

my @in="sample.md".IO.lines;
my \[email protected];
my @out;
my $k = 0;

while ($k < len) {
    if @in[$k] ~~ / ^ '{:lang="' (\w+) '"}' $ / { 
    push @out, "```$0";
    $k++;
    while @in[$k].starts-with("    ") {
        push @out, @in[$k].trim-leading;
        $k++;   }
    push @out, "```";
    }
    push @out, @in[$k];
    $k++;
}

for @out {print "$_\n"}
raiph

Using a Perl regex

There are many dialects of regex. The regex pattern you've used is a Perl regex but you haven't told Raku that. So it's interpreting your regex as a Raku regex, not a Perl regex. It's like feeding Python code to perl. So the error message is useless.


One option is to switch to Perl regex handling. To do that, this code:

      /^{:lang="([a-z]+)"}$/

needs m :P5 at the start:

m :P5 /^{:lang="([a-z]+)"}$/

The m is implicit when you use /.../ in a context where it is presumed you mean to immediately match, but because the :P5 "adverb" is being added to modify how Raku interprets the pattern in the regex, one has to also add the m.

:P5 only supports a limited set of Perl's regex patterns. That said, it should be enough for the regex you've written in your question.

Using a Raku regex

If you want to use a Raku regex you have to learn the Raku regex language.

The "spirit" of the Raku regex language is the same as Perl's, and some of the absolute basic syntax is the same as Perl's, but it's different enough that you should view it as yet another dialect of regex, just one that's generally "powered up" relative to Perl's regexes.

To rewrite the regex in Raku format I think it would be:

/ ^ '{:lang="' ([a..z]+) '"}' $ /

(Taking advantage of the fact whitespace in Raku regexes is ignored.)

Other problems in your code

After fixing the regex, one encounters other problems in your code.

The first problem I encountered is that $key is read-only, so $key++ fails. One option is to make it writable, by writing -> $key is copy ..., which makes $key a read-write copy of the index passed by the .kv.

But fixing that leads to another problem. And the code is so complex I've concluded I'd best not chase things further. I've addressed your immediate obstacle and hope that helps.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Raku Regex to capture and modify the LFM code blocks

From Dev

Regex: capture multiple similar blocks

From Dev

Python regex for blocks of code?

From Java

how to interpolate string containing capture-group parentheses as regex in Raku?

From Dev

Modify Code Completion Settings in Code::Blocks

From Dev

"inconsistent" match result when using code block in regex [Raku]

From Dev

Regex for converting indented lines into code blocks

From Dev

Regex to capture code comments not working when * followed by /

From Java

Parse URLs using Regex, Ignoring Code Blocks and Code Snippets in Markdown

From Dev

Modify code to capture values greater than - instead of exact match

From Dev

Is there an easy Regex expression to capture a reference code that continuously changes?

From Dev

Functional programming in c#: how do I nest using statements and allow code blocks to modify returned data?

From Dev

Working with Unicode Blocks in Regex

From Dev

Regex to match blocks of text

From Dev

How to create "blocks" with Regex

From Java

Raku: Using topic variable (from a 'for') inside a regex

From Java

Raku regex: Inconsistent longest token matching

From Dev

Is it possible to do boolean assertions with raku regex?

From Dev

How to modify a lambda pointer capture?

From Dev

multiple group capture/modify with sed

From Dev

RegEx in Notepad++ - How to get specific line segments in repeating code blocks looking like this?

From Dev

Capture functions using regex

From Java

Regex to capture pattern with sql

From Dev

Regex to capture GPA with Pyhton

From Dev

Regex capture group swift

From Dev

Regex to capture spaces AND commas?

From Dev

regex capture repeated phrases

From Dev

Capture string in regex replacement

From Dev

capture with regex in javascript

Related Related

  1. 1

    Raku Regex to capture and modify the LFM code blocks

  2. 2

    Regex: capture multiple similar blocks

  3. 3

    Python regex for blocks of code?

  4. 4

    how to interpolate string containing capture-group parentheses as regex in Raku?

  5. 5

    Modify Code Completion Settings in Code::Blocks

  6. 6

    "inconsistent" match result when using code block in regex [Raku]

  7. 7

    Regex for converting indented lines into code blocks

  8. 8

    Regex to capture code comments not working when * followed by /

  9. 9

    Parse URLs using Regex, Ignoring Code Blocks and Code Snippets in Markdown

  10. 10

    Modify code to capture values greater than - instead of exact match

  11. 11

    Is there an easy Regex expression to capture a reference code that continuously changes?

  12. 12

    Functional programming in c#: how do I nest using statements and allow code blocks to modify returned data?

  13. 13

    Working with Unicode Blocks in Regex

  14. 14

    Regex to match blocks of text

  15. 15

    How to create "blocks" with Regex

  16. 16

    Raku: Using topic variable (from a 'for') inside a regex

  17. 17

    Raku regex: Inconsistent longest token matching

  18. 18

    Is it possible to do boolean assertions with raku regex?

  19. 19

    How to modify a lambda pointer capture?

  20. 20

    multiple group capture/modify with sed

  21. 21

    RegEx in Notepad++ - How to get specific line segments in repeating code blocks looking like this?

  22. 22

    Capture functions using regex

  23. 23

    Regex to capture pattern with sql

  24. 24

    Regex to capture GPA with Pyhton

  25. 25

    Regex capture group swift

  26. 26

    Regex to capture spaces AND commas?

  27. 27

    regex capture repeated phrases

  28. 28

    Capture string in regex replacement

  29. 29

    capture with regex in javascript

HotTag

Archive