Posts: 92
Threads: 30
Joined: Oct 2017
hello all ...
i have this string :
Quote:<?xml version="1.0" encoding="utf-8"?>
<metafile version="3.6" client="site">
<name>English (en-GB)</name>
<version>3.6.0</version>
<creationDate>July 2016</creationDate>
<author>Joomla! Project</author>
<authorEmail>admin@joomla.org</authorEmail>
<authorUrl>www.joomla.org</authorUrl>
<copyright>Copyright © 2005 - 2016 Open Source Matters. All rights reserved.</copyright>
<license>GNU General Public License version 2 or later; see LICENSE.txt</license>
<description>en-GB site language</description>
<metadata>
<name>English (en-GB)</name>
<tag>en-GB</tag>
<rtl>0</rtl>
<locale>en_GB.utf8, en_GB.UTF-8, en_GB, eng_GB, en, english, english-uk, uk, gbr, britain, england, great britain, uk, united kingdom, united-kingdom</locale>
<firstDay>0</firstDay>
<weekEnd>0,6</weekEnd>
</metadata>
<params />
</metafile>
i need to search for <version> then print just ( 3.6.0 ) , how i can do that ?
Posts: 7,192
Threads: 124
Joined: Sep 2016
Posts: 92
Threads: 30
Joined: Oct 2017
(Oct-22-2019, 06:54 AM)buran Wrote: what have you tried? match = re.compile(r'"<version>"\:(".*?)["\[]')
a = match.findall(q)
print(a) i got
Output: []
Posts: 7,192
Threads: 124
Joined: Sep 2016
https://docs.python.org/3.7/library/xml....ttree.html
or use some external module as lxml or BeautifulSoup
Posts: 195
Threads: 21
Joined: Aug 2019
(Oct-22-2019, 06:53 AM)evilcode1 Wrote: i have this string :
Quote:<?xml version="1.0" encoding="utf-8"?>
<metafile version="3.6" client="site">
<name>English (en-GB)</name>
<version>3.6.0</version>
<creationDate>July 2016</creationDate>
<author>Joomla! Project</author>
[ ... ]
i need to search for <version> then print just ( 3.6.0 ) , how i can do that ?
Hi!
Maybe you could use something like:
import re
string1 = '''<?xml version="1.0" encoding="utf-8"?>
<metafile version="3.6" client="site">
<name>English (en-GB)</name>
<version>3.6.0</version>
<creationDate>July 2016</creationDate>
<author>Joomla! Project</author>
[ ... ]'''
requirement1 = re.search('<version>(.*)</version>', string1)
print(requirement1.group(1)) that produces the following output:
Output: 3.6.0
All the best,
Posts: 92
Threads: 30
Joined: Oct 2017
Oct-22-2019, 07:40 AM
(This post was last modified: Oct-22-2019, 07:41 AM by evilcode1.)
(Oct-22-2019, 07:30 AM)newbieAuggie2019 Wrote: (Oct-22-2019, 06:53 AM)evilcode1 Wrote: i have this string :
i need to search for <version> then print just ( 3.6.0 ) , how i can do that ?
Hi!
Maybe you could use something like:
import re
string1 = '''<?xml version="1.0" encoding="utf-8"?>
<metafile version="3.6" client="site">
<name>English (en-GB)</name>
<version>3.6.0</version>
<creationDate>July 2016</creationDate>
<author>Joomla! Project</author>
[ ... ]'''
requirement1 = re.search('<version>(.*)</version>', string1)
print(requirement1.group(1)) that produces the following output:
Output: 3.6.0
All the best, thank u very much <3 works and this is better than starting coding new code with xml.etree.ElementTree
(Oct-22-2019, 07:05 AM)buran Wrote: https://docs.python.org/3.7/library/xml....ttree.html
or use some external module as lxml or BeautifulSoup
thank u for quick response , but i will prefer regex and @ newbieAuggie2019 give me a solution
Posts: 195
Threads: 21
Joined: Aug 2019
Oct-22-2019, 07:44 AM
(This post was last modified: Oct-22-2019, 07:44 AM by newbieAuggie2019.)
(Oct-22-2019, 07:40 AM)evilcode1 Wrote: thank u very much <3 works
You're welcome!
Posts: 7,192
Threads: 124
Joined: Sep 2016
why not parse html/xml with regex is discussed so many times... use proper tools for the task or it will bite you when you don't expect
Posts: 195
Threads: 21
Joined: Aug 2019
(Oct-22-2019, 07:40 AM)evilcode1 Wrote: (Oct-22-2019, 07:05 AM)buran Wrote: https://docs.python.org/3.7/library/xml....ttree.html
or use some external module as lxml or BeautifulSoup
thank u for quick response , but i will prefer regex
(Oct-22-2019, 07:50 AM)buran Wrote: why not parse html/xml with regex is discussed so many times... use proper tools for the task or it will bite you when you don't expect
Hi again!
Although I always try to address a problem from the point of view and knowledge of a newbie like myself, if anyone from this site advises not to use what I have suggested in good faith, I would take their advice into consideration, as their knowledge is much much deeper than mine.
All the best,
|