Affichage des articles dont le libellé est expressions. Afficher tous les articles
Affichage des articles dont le libellé est expressions. Afficher tous les articles

mardi 18 mars 2014

Re: Find and replace multiple RegEx search expressions topic




Jignesh Sutar wrote:

> Hi,
>
> I'm trying to delete contents of a .txt log file, matching on multiple
> re.sub criteria but not sure how to achieve this.
>
> Below is an illustration of what I am trying to achieve (of course in this
> example I can combine the 3 re.sub into a single re expression but my
> actual code will have a dozen plus expression I need to match on so easier
> to keep them separate). Only the last re.sub will take effect in the
> example below I need all 3 to take effect.
>
>
> import re
> o = open(r"c:\temp\outputfile.txt","w")
> data = open(r"C:\Temp\infile.txt").read()
> o.write( re.sub(".*<X> ","",data) )
> o.write( re.sub(".*<Y> ","",data) )
> o.write( re.sub(".*<Z> ","",data) )
> o.close()


Apply all substitutions to data before you write the result to the file:

with open(infile) as f:
data = f.read()

for expr in list_of_regexes:
data = re.sub(expr, "", data)

with open(outfile, "w") as f:
f.write(data)







Find and replace multiple RegEx search expressions topic




Hi,

I'm trying to delete contents of a .txt log file, matching on multiple
re.sub criteria but not sure how to achieve this.

Below is an illustration of what I am trying to achieve (of course in this
example I can combine the 3 re.sub into a single re expression but my
actual code will have a dozen plus expression I need to match on so easier
to keep them separate). Only the last re.sub will take effect in the
example below I need all 3 to take effect.


import re
o = open(r"c:\temp\outputfile.txt","w")
data = open(r"C:\Temp\infile.txt").read()
o.write( re.sub(".*<X> ","",data) )
o.write( re.sub(".*<Y> ","",data) )
o.write( re.sub(".*<Z> ","",data) )
o.close()


Thanks in advance.
Jignesh






jeudi 23 janvier 2014

building "path expressions" topic




On 1/23/2014 11:37 AM, Stefan Ram wrote:
> When creating an interface for a library, sometimes one wants the user
> to see nesting in a »path expression«, like for example »a.b.c.d.e.f.g.h«
> to convey the idea that h is a part of g, which is a part of f, and so on.
>
> I think it is funny that one can use either package names /or/ nested
> classes /or/ fields to implement such a path, without this choice being
> visible in the »path expression«. For example, one posssibility to
> implement a »path expression« »a.b.c.d.e.f.g.h« might be:
>
> package a.b.c;
> class G { int h = 8; }
> class F { G g = new G(); }
> class d { static class e { static F f = new F(); }}
> public final class Main
> { public static void main( String[] args )


ITYM "java.lang.String[]", also "class a.b.c.G",
"a.b.c.G g = new a.b.c.G();", etc. ;-)

> { java.lang.System.out.println( a.b.c.d.e.f.g.h ); }}


If it seems funny to you, your sense of humor is more
finely developed than mine. :)

Just for the record, note that the "a.b.c" part is
different from the rest. Packages have no hierarchical
relationship, that is, package "a.b.c" is unrelated to
"a.b.x" and even to "a.b". To put it another way, the
path "a.b.c.d.e.f.g.h" has six elements ("a.b.c" and five
more), not eight.

--
Eric Sosman
(E-Mail Removed)d