regex - Perl: Can I use capture groups in variables used in text replace regular expressions? -


Just for fun, I'm very new to Perl and I'm trying to write a simple text processing tool, But I 'm going to read the rules of the tool stuck in a simple thing, from a simple text file (not to the script, and perhaps it is important), to process a text file, there is an array of patterns / patterns in pairs (Like each for each process Yum line ). Here is the sub to apply the changes:

  My ($ text, @ rule) = @_; My @ lines = partition (/ \ n /, $ text); Forex Currency (@Rules) {My $ pattern = $ _- & gt; {"Pattern"}; My $ substituted = $ _- & gt; {"Substituted"}; $ Rows = map {$ _ = ~ S / $ pattern / $ substituted / g; } @ Lines; } Join the return ("\ n", @lines); For example, if there is a rule like  pattern = [aeiou]  +  replace = * , then the text  foo bar  to  F ** B * r . That's what i want 

Although I do not know why I can not use the capture group to change the text content, pattern = ([FF]) + <= > <0> <1> <1> in <=> <1> me Oh times I think I'm missing a very simple thing, what am I missing?

UPDATE:

After some experiments, my whole result is:

  sub escapists litteral {my ($ Literal) = @_; $ Literal = ~ s / \ // \\\ // g; Literally $; } Sub sub (my ($ pattern, $ change, $ modifier) ​​= @_; $ modifier || = ''; my $ expression = '$ text = ~ s /' escapeswist literal ($ pattern) '/' escapeswist literal ($ Changed). '/' $ Modifier; return sub {my ($ text) = @_; Eval $ expression; $ text;};} $ CustomSubst = subst ($ pattern, $ place, $ modifiersToken); $ Foo = $ customSubst- & gt; ($ foo); $ Bar = $ customSubst- & gt; ($ bar);  

If your replacement string includes capture variables, then you evaluate this as a string Therefore, it should be attached in double quote and the replacement is required to double eval if you already avoid any double quotes, which are already present, then it will work in that manner, whether it is in There should be a capture variable.

Something like this should be appropriate. Well, I'm not sure that the string is different in line before replacing the string without the / s modifier How helpful is the Industry Ministers, it will only make a difference to the very obscure patterns.

  Use Strict; Use warnings; Use 5.010; My Rule = ({Pattern = & gt; '[AEUU]', Replacement = & gt; '*',}, {pattern = & gt; '([FF])', Replacement => & lt ; $ 1 & gt; ',}}); Replacement ('then the text is processed in fu bar f ** b * r', @rr); Sub (My Text (Text, Rule) = @_; My @ Lines = Split / \ n /, $ text; {My ($ pattern, $ replaced) = $ {$ rule} for my $ rule (@Rules) } {Change qw / pattern}; instead of $ = ~ s / "/ \\" / g; S / $ pattern- '' '.' Replace '' '' / gee @lines;} "\ N", @ lines Join in;}  

Output

  th * nt * xt  F & gt; ** b * R * s pr * c * Ss * d * nt *  ** b * r  

Comments

Popular posts from this blog

java - org.apache.http.ProtocolException: Target host is not specified -

java - Gradle dependencies: compile project by relative path -

ruby on rails - Object doesn't support #inspect when used with .include -