-2

I have following xml tags in my xml file as follows '''

<pd:link scheme="http://www.w3.org/1999/xhtml" target="www.altruvest.org <pd:unicode ch="2014"/> or <pd:unicode ch="2014"/> www.Boardmatch.org">"www.altruvest.org <pd:unicode ch="2014"/> or <pd:unicode ch="2014"/> www.Boardmatch.org</pd:link>) '''

in above tag pd:unicode tag is inside text value of target. I want to create regular expression pattern to find such tag where tag is within text in python.

Can anyone please help to create pattern for this?

Yogesh
  • 1
  • 1
  • 1
    Unclear what your tags look like. Please provide a more comprehensive example of your input, and try to describe exactly what tags you're looking for. If you don't know the language of regular expressions, try to describe it in English, like : "The word 'pig' or the word 'dog' surrounded by square brackets, at the end of a line". – machine yearning Jul 18 '11 at 07:59
  • 2
    Nobody could understand your question because you did not format your code, and therefore the tags you put were invisible. Please read this site FAQ and learn to use markdown (the SO's formatting syntax). It takes a minute or so!! – mac Jul 18 '11 at 08:54

1 Answers1

2

Edited answer:

>>> s = r'"<pd:link scheme="http://www.w3.org/1999/xhtml" target="www.altruvest.org <pd:unicode ch="2014"/> or <pd:unicode ch="2014"/> www.Boardmatch.org">www.altruvest.org <pd:unicode ch="2014"/> or <pd:unicode ch="2014"/> www.Boardmatch.org</pd:link>"'
>>> import re
>>> r = re.search(r'=".*?(<pd:unicode ch="\d+"/>).*?"', s, re.DOTALL)
>>> r.groups()
('<pd:unicode ch="2014"/>',)

What the above does is to match the pd:unicode tags when they are preceded by a =" and followed by ". The re.DOTALL ignores newlines (treats them as normal characters).

Bare in mind that what you are asking to do is parsing XML, something for which you should use an xmlparser (see for example xml.etree or a more general discussion here), and not regular expressions. Accurately parsing XML by mean of regex is actually not possible, so the above regex is likely to generate false positives or to miss some true ones.

If you don't want to go with a full XML parser, you could consider something like pyparsing instead.

Community
  • 1
  • 1
mac
  • 42,153
  • 26
  • 121
  • 131
  • No, I want to find `` tag if it's within text such as `` in above case pd:unicode tag is within text value. – Yogesh Jul 18 '11 at 09:56
  • @Yogesh - Not hearing back from you. Did the edited answer... answered your question? If no, please provide feedback, if yes, please mark as accpeted! :o – mac Jul 21 '11 at 11:59