Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Regex
#5
Just to show one way,it's not normal to parse all input when have markup input.
Think of a web-site of XML doc that can have 100/1000 of tags,then want to narrow it down to find parse info needed.

There had been no problem to solve this specific task with with regex,
it just that when see markup there is a better way which is using a parser.
Also as info Regex is supported for use inside both BeautifulSoup and lxml.
Eg:
soup.find_all(re.compile("^b")) .
//a[re:test(@id, "^hypProduct_[0-9]+$")]
from bs4 import BeautifulSoup

data = '''\
<Color>blue</Color>
<Code>1</Code>
<Color>red</Color >
<Code>2</Code>'''

soup = BeautifulSoup(data, 'lxml')
for item in soup.find_all(['color', 'code']):
    print(f'{item.name.capitalize()}:{item.text}')
Output:
Color:blue Code:1 Color:red Code:2
ichabod801 Wrote:I think maybe that's overstating it a little bit.
Sure it is,but it's one the best and funny post about this topic.
I have like to that post bye @bobince many times over the years.
Reply


Messages In This Thread
Regex - by yuyu - Dec-13-2018, 08:57 PM
RE: Regex - by ichabod801 - Dec-13-2018, 09:10 PM
RE: Regex - by nilamo - Dec-13-2018, 10:56 PM
RE: Regex - by ichabod801 - Dec-14-2018, 03:25 AM
RE: Regex - by snippsat - Dec-14-2018, 02:43 PM
RE: Regex - by yuyu - Dec-15-2018, 11:11 PM
RE: Regex - by ichabod801 - Dec-16-2018, 12:03 AM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020