Mercurial > repos > kellrott > regex_replace
comparison regex_replace.xml @ 0:9a77d5fca67c draft default tip
Uploaded
author | kellrott |
---|---|
date | Mon, 19 Nov 2012 01:04:06 -0500 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:9a77d5fca67c |
---|---|
1 <tool id="regex_replace" name="Regex Replace" version="1.0.0"> | |
2 <description>Regular Expression replacement using the Python re module</description> | |
3 <command interpreter="python">$script_file</command> | |
4 <inputs> | |
5 <param name="infile" type="data" label="Input File"/> | |
6 <param name="search_str" type="text" size="70" label="Search String"> | |
7 <sanitizer> | |
8 <valid initial="string.printable"> | |
9 <remove value="""/> | |
10 <remove value="\"/> | |
11 </valid> | |
12 <mapping initial="none"> | |
13 <add source=""" target="\""/> | |
14 <add source="\" target="\\"/> | |
15 </mapping> | |
16 </sanitizer> | |
17 </param> | |
18 <param name="replace_str" type="text" size="70" label="Replace String"> | |
19 <sanitizer> | |
20 <valid initial="string.printable"> | |
21 <remove value="""/> | |
22 <remove value="\"/> | |
23 </valid> | |
24 <mapping initial="none"> | |
25 <add source=""" target="\""/> | |
26 <add source="\" target="\\"/> | |
27 </mapping> | |
28 </sanitizer> | |
29 </param> | |
30 <param name="multiline" type="boolean" label="Multi-line" checked="False"/> | |
31 <param name="ignore_case" type="boolean" label="Ignore Case" checked="False"/> | |
32 <param name="dot_all" type="boolean" label="Dot All" checked="False"/> | |
33 <param name="replace_count" type="integer" label="Replace Count(0=all)" value="0"/> | |
34 </inputs> | |
35 <outputs> | |
36 <data name="outfile" format="txt"/> | |
37 </outputs> | |
38 <configfiles> | |
39 <configfile name="script_file"><![CDATA[#!/usr/bin/env python | |
40 | |
41 import re | |
42 | |
43 search_str = """${search_str}""" | |
44 replace_str = """${replace_str}""" | |
45 | |
46 opts = 0 | |
47 #if $ignore_case: | |
48 opts |= re.IGNORECASE | |
49 #end if | |
50 #if $multiline: | |
51 opts |= re.MULTILINE | |
52 #end if | |
53 #if $dot_all: | |
54 opts |= re.DOTALL | |
55 #end if | |
56 cmd = re.compile(search_str, opts) | |
57 | |
58 ihandle = open("""${infile}""") | |
59 ohandle = open("""${outfile}""", "w") | |
60 | |
61 #if $multiline: | |
62 itext = ihandle.read() | |
63 otext = cmd.sub(replace_str, itext, count=$replace_count ) | |
64 ohandle.write(otext) | |
65 #else | |
66 for line in ihandle: | |
67 oline = cmd.sub(replace_str, line, count=$replace_count ) | |
68 ohandle.write(oline) | |
69 #end if | |
70 | |
71 ihandle.close() | |
72 ohandle.close() | |
73 | |
74 ]]></configfile> | |
75 </configfiles> | |
76 | |
77 <help> | |
78 Perform regular expression replacement using the Python re engine. | |
79 | |
80 Example: | |
81 ======== | |
82 Search String:: | |
83 | |
84 [^\s]+_(\w+).* | |
85 | |
86 Replace String:: | |
87 | |
88 \1 | |
89 | |
90 On the input file:: | |
91 | |
92 LJP001_BT20_24H:DMSO | |
93 LJP001_BT20_24H:H20 | |
94 LJP001_BT20_6H:DMSO | |
95 | |
96 Outputs:: | |
97 | |
98 24H | |
99 24H | |
100 6H | |
101 | |
102 Additional Options | |
103 ================== | |
104 | |
105 Replace Count: | |
106 Will define the number of occurrences that can be replaced by the substitution. A count of 0 is equivalent | |
107 to 'replace all' | |
108 | |
109 | |
110 Ignore Case: | |
111 Make searches case insensitive | |
112 | |
113 Multi-line: | |
114 Do the search across the entire contents of the file. If not checked, then replacements occur line by line | |
115 | |
116 Dot-All: | |
117 With this checked, the '.' in a matching pattern will also match new-line characters. Generally used with 'multi-line' search | |
118 | |
119 </help> | |
120 </tool> |