![]() |
Perl regex not matching across multiple lines despite ms flags
Hi all,
I have written a regular expression (tested in regexpal and regextester alpha something) with which I want to replace something like: Code:
myFunction: function myFunction(arg1, arg2) {Code:
myFunction: (function myFunction ...Code:
([A-Za-z0-9_]+):\s?function\s?\1?\(([A-Za-z0-9_\ ,]*)\) {(\n?((.*)\n\t?)*)}Code:
$1: (function $1($2) {$3})Code:
#! /bin/bashThis is really frustrating me so I'd love some help. |
We should first verify that it's not a syntax problem. Try this mod:
Code:
#! /bin/bash |
Quote:
Code:
myFunction: function myFunction(arg) {...},Code:
myFunction: (function myFunction(arg) {...}), |
Try this one
Code:
#! /bin/bash |
Aargh, konsolebox just beat me to almost the exact same solution. And I spent all morning trying to figure it out, since I have little direct experience with perl. :banghead: (Indeed, I still don't know exactly what undef $/ does, only that it seems to work.)
One thing I can add though is that I read that \w equals [[:alnum:]_], so you should be able to use that instead. This is what I came up with in the end: Code:
perl -pe 'undef $/; s/(\w):\s?(function\s?\1?([^}]+)})/\1: (\2)/gms' |
That was only a difference in time. I also don't know much about perl re (not even noticed \w) so I searched the web for answers. That icon's really funny btw. :D
|
No sweat there. It was a good learning experience for me. It's just a bit of a letdown to finally figure something out, only to be beaten by just a few minutes.
Now I know of a new way to handle multi-line matches. If only sed would introduce similar flags.... |
I searched again. It appears that it can also be done in sed:
Code:
sed -n ' |
Hi guys,
I really appreciate all the responses but the regex I originally supplied: Code:
([A-Za-z0-9_]+):\s?function\s?\1?\(([A-Za-z0-9_\ ,]*)\) {(\n?((.*)\n\t?)*)}Code:
aFunction: function aFunction() { |
But even if it's not the case that perl properly recognize newline and tab chars, this expresion could still match more than just a single function
Code:
{(\n?((.*)\n\t?)*)}Code:
... { |
Thought I would put in 2C if not perl:
Code:
awk 'BEGIN{RS=""}/function/{sub(/function/,"(&");$NF=$NF ")"}1' file |
Quote:
@grail: You'll have to explain that to me... |
Quote:
It sets the Record Separator to be an empty line and then searches for any record containing the word function. Once found it prepends an opening bracket to the start of the word 'function' and then to the last field it appends a closing bracket. The 1 at the end prints everything it runs into so you can transfer all contents to a new file. If you are replacing the original you will need to perform a move on completion. |
@grail So that's the power of RS. I never thought that awk can do that. That should be helpful with the improvement of my compiler :D.
--- a wrong idea --- |
Quote:
Code:
someName: function someName... } |
| All times are GMT -5. The time now is 11:27 PM. |