Python Forum
Thread Rating:
  • 1 Vote(s) - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Regex search for string
#1
Hello,

I'm trying to formulate a regex search to see if this exact string, ''op_single_selector" exists in this line of text:

-> [!div class="op_single_selector"]

This is what I have now that's not working:

selector_string = False
single_selector_regex_search_string = re.compile(r"[\+-]>\s\[op_single_selector\]")
if single_selector_regex_search_string.search(file_information.patch):
    selector_string = True
Reply
#2
(Feb-06-2017, 08:33 PM)DBS Wrote: Hello,

I'm trying to formulate a regex search to see if this exact string, ''op_single_selector" exists in this line of text:

-> [!div class="op_single_selector"]

This is what I have now that's not working:

selector_string = False
single_selector_regex_search_string = re.compile(r"[\+-]>\s\[op_single_selector\]")
if single_selector_regex_search_string.search(file_information.patch):
    selector_string = True

Are you trying to parse HTML with regex?

You can just use the in operator.

if target in search_string:
However if its HTML, then something like BeautifulSoup is better.
from bs4 import BeautifulSoup

data = '''
<html>
    <body>
        <div class="op_single_selector">
        content
        </div>
    </body>
</html>
'''
soup = BeautifulSoup(data, 'html.parser')
search = soup.find('div', class_='op_single_selector')
if search:
    #there is a class op_single_selector
or similar
print('op_single_selector' in soup.div['class'])
Output:
True
Recommended Tutorials:
Reply
#3
I'm actually trying to parse a markdown file associated with a pull request using the GitHub API.
Reply
#4
(Feb-06-2017, 08:33 PM)DBS Wrote: Hello,

I'm trying to formulate a regex search to see if this exact string, ''op_single_selector" exists in this line of text:

-> [!div class="op_single_selector"]

This is what I have now that's not working:

selector_string = False
single_selector_regex_search_string = re.compile(r"[\+-]>\s\[op_single_selector\]")
if single_selector_regex_search_string.search(file_information.patch):
    selector_string = True

Not surprising, if only because you are trying to match:
  • "op with [op 
  • or"] with or]
among other things... since I cannot fathom what you are trying to match with [\+-]>\s

IMHO, better use the markdown utility to convert the whole thing to full HTML and handle the HTML with an HTML parser.
Unless noted otherwise, code in my posts should be understood as "coding suggestions", and its use may require more neurones than the two necessary for Ctrl-C/Ctrl-V.
Your one-stop place for all your GIMP needs: gimp-forum.net
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  string parsing with re.search() delahug 9 3,502 Jun-04-2020, 07:02 PM
Last Post: delahug
  [Learning:bs4, re.search] - RegEx string cutoff jarmerfohn 5 3,581 Nov-23-2019, 09:32 AM
Last Post: buran

Forum Jump:

User Panel Messages

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