For my task I needed to know where the 'bad' substring was. One problem with this approach is that it doesn't tell you the position in the original string where you substrings occur. There are numerous other solutions to this problem using negative look ahead assertions Position of the match , then you can combine them together with |'s, i.e. If you have several 'known good' patterns, and you want to find all the substrings that don't match A, or B, or C. There might be empty strings ( ''), you probably should ignore them. # match now holds some text that doesn't match the regex The text returned is the substrings that don't match your regular expression!ĭoc = "'Hello there', he said, 'what do you need?'." The matched text is not returned, only the rest of the text. You can then use python's re.split(.) function to split the string based on this regular expression. It's relatively easy to make a regex that extracts all the quoted text: r"'.*?'" (Note: this is python re syntax). You'll need a more powerful tool than regexes for that This is a simple example, and this ignores nested quotes. and I want to find all the non-quoted text (i.e. For example if I had the string 'Hello there', he said, 'what do you need?'. I knew what would be a good substring, I could write regular expressions for them. What parts of a string don't match a regex? What parts of a string don't match a regex?įor recent task I had a big string and I wanted to find 'bad' substrings.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |