annotate regex_replace.xml @ 0:9a77d5fca67c draft default tip

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