473,397 Members | 2,084 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

How to replace an attribute with empty string using regex?

ashitpro
542 Expert 512MB
I am parsing one xml, I want to replace an attribute with empty string.

every node has an attribute, something like this:

id="1hyx36uhpi780iq8oiu355"

I am using following regex pattern, but its not working

d = re.search('(id="([a-z0-9A-Z]+)")*',text)
Nov 2 '10 #1
2 2314
bvdet
2,851 Expert Mod 2GB
In what way is it not working?

Are you parsing the XML with an XML parser such as minidom? If not, you should consider doing so.

If parsing the string directly, have you tried parsing it line by line?

This seems to work:
Expand|Select|Wrap|Line Numbers
  1. >>> import re
  2. >>> patt = re.compile(r'id="[a-z0-9A-Z]+"')
  3. >>> s = 'id="1hyx36uhpi780iq8oiu355"   xxxxx xxxxx id="46fhrt5976jkfjhrh"'
  4. >>> s1 = patt.sub('id=""', s)
  5. >>> s1
  6. 'id=""   xxxxx xxxxx id=""'
  7. >>> 
Nov 2 '10 #2
Oralloy
988 Expert 512MB
ashtipro,

It looks like you've got a problematic indefinite repetition operator in your statement:

Expand|Select|Wrap|Line Numbers
  1. d = re.search('(id="([a-z0-9A-Z]+)")*',text)
  2.     HERE  --------------------------^
  3.  
Which I think will cause undesired matches of zero length.

Also, I don't know what your data stream looks like, however you may need to check for single quotes (')enclosing the attribute value, as well.

Luck!
Nov 2 '10 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: MAJED ASSAF | last post by:
hello There Please I need help !! I don't know why my command prompt won't change to python even if I set the path it stays like the following Microsoft Windows XP (C) Copyright 1985-2001...
75
by: Xah Lee | last post by:
http://python.org/doc/2.4.1/lib/module-re.html http://python.org/doc/2.4.1/lib/node114.html --------- QUOTE The module defines several functions, constants, and an exception. Some of the...
1
by: DB_2 | last post by:
Greetings, I was searching Google for ways to turn off transaction logging for some queries. I came across this old post from Feb 2003: > From: fareeda (fareeda@pspl.co.in) > Subject: Re:...
2
by: archana | last post by:
Hi all, I don't have much of knowledge about threading. I have on .net application where in main i am starting one secondary thread. like
2
by: bruce | last post by:
hi... does python provide regex handling similar to perl. can't find anything in the docs i've seen to indicate it does... -bruce
8
by: Xah Lee | last post by:
the Python regex documentation is available at: http://xahlee.org/perl-python/python_re-write/lib/module-re.html Note that, i've just made the terms of use clear. Also, can anyone answer what...
0
by: Gary Herron | last post by:
Ohad Frand wrote: There is no way you can consider 'elif', 'else', 'except', and 'from' statements. However, as someone pointed out, the kwlist from the keyword module is the closest thing we...
0
by: Ohad Frand | last post by:
Hi Thanks a lot for your reply I think the main uses for it is to study the language and to see that I didn't miss anything else or that something is changed from one version to another. The...
0
by: Tim N. van der Leeuw | last post by:
Hey Gerhard, Gerhard Häring wrote: I so far forgot to say a "thank you" for the suggestion :-) The sample code as you sent it doesn't do what I need to do, but I did look at it for...
3
by: MalteseUnderdog | last post by:
Hi there I just started python (but this question isn't that trivial since I couldn't find it in google :) ) I have the following text file entries (simplified) start #frag 1 start x=Dog...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.