Help with vim regular expression
I've been trying to use this command
Code:
:%s/\(src|href)="\(assets\/.+?\)/\1="{% static '\2' %}"/g What I've been trying to do is to change all the Code:
href=" (static file directory) " Code:
(href/src)="{% static "(page source)" %}" Notes: By the way, please forgive me for my lack of knowledge of vim, I've hardly ever used it before. I'm using it now since it seems to be the quickest way to solve my problem, but if you have any other suggestions, please feel free to let me know. |
Quote:
By the way, I believe that you can't nest \(...\). An expression like \(....\(...\)...\) is illegal. Quote:
Code:
1,$s/href=" (static file directory) "/(href\/src)="{% static "(page source)" %}/ Quote:
|
Quote:
Code:
:%s/\(src|href\)="\(assets\/.+?\)/\1="{% static '\2' %}"/g Code:
E486: Pattern not found: \(src|href\)="\(assets\/.+?\) Quote:
Code:
E486: Pattern not found: href=" (static file directory) " Code:
<link rel="stylesheet" href="style/login.css"> Code:
<link rel="stylesheet" href="{% static "style/login.css" %}"> |
The problem is not your lack of knowledge of vim per-se, but your ignorance of regex. For vim you can get a cheat-sheet and get up to speed easily and quickly; regex is akin to voodoo for the naif.
You have to specifically mould it for your data - if it doesn't exactly fit the input for the answer you found (we now know it doesn't), it won't work. But can be easily adapted as @berndbausch attempted above. But for that we need to know input data structure - examples in full; "there could be lines that look like this" won't suffice. Ditto for expected output. Personally I'd use sed - it's much more obvious what's happening IMHO. |
Quote:
The idea is to find all the lines with a "src" or "href" tags on the html templates, which will look like so Code:
<link rel="stylesheet" href="style/login.css"> The '"{% static' and '%}"' need to enclose the '"style/login.css"' directory so Django will know what files the 'href' tag is referring to. So, the final line should look like this Code:
<link rel="stylesheet" href="{% static "style/login.css" %}"> Code:
From: |
sed is a "stream editor" - you feed it a file(s) and tell it what to do with it. Regex like you found is just the ticket. It will spit out modified lines that match, or else the input record unmodified.
The issue with the solution you found is that it was structured for hrefs that contain the literal string "assets" - so even if the syntax error wasn't there, it was only indicative for you. The explanation in the link is a good synopsis. However ... using regex you have to define the data precisely. In the original answer it was "assets" and everything that followed it - then add the trailing "%}". Several problems with your data - not all records have the same structure (first record doesn't has css/ as leading dir. Some records have trailing "rel=" keyword, first one doesn't. Can be accommodated, but makes the regex more difficult. Consistency counts for a lot. This seems to handle the (limited) input provided. Code:
sed -r 's/(src|href)="([^[:space:]>]+?)/\1="{% static \2 %}"/' your_file.here |
Quote:
Quote:
Code:
ed -r -i 's/(src|href)="([^[:space:]>]+?)/\1="{% static "\2 %}"/' By the way, here is a script I wrote (again, pretty simple stuff, but it's good to have a copy-paste solution) to make html templates work with Django's static files directory, just in case someone needs it Code:
#!/bin/bash |
Hint: use "\v" (very magic mode) so that you don't need as much escaping.
http://andrewradev.com/2011/05/08/vim-regexes/ There's a missing backslash very, very early in the expression at the top of your first post. |
Quote:
Quote:
|
Code:
:%s/\(src|href) I see you corrected that in later posts, but the point was that you were less likely to make that mistake with very-magic mode on. |
At the risk of derailing the topic: the internet is full of warnings against parsing html/xml with regular expressions.
|
All times are GMT -5. The time now is 08:42 PM. |