重新编译与我的字符串不匹配 [英] re.compile not matching my string

查看:49
本文介绍了重新编译与我的字符串不匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码:

def split(content):
   pattern = re.compile(r"""(\\\[-16pt]\n)(.*?)(\n\\\nthinhline)""", re.X | re.DOTALL)
   print(pattern.finditer(content))
   for m in pattern.finditer(content):
       print ("in for loop")
       print("Matched:\n----\n%s\n----\n" % m.group(2))
   print ("in split")


def replacement(content):
   split(content)
   pattern = re.compile(r'(?<=\\\\\[-16pt]\n)([\s\S]*?)(?=\\\\\n\\thinhline)')
   content= ' '.join(re.findall(pattern, content))
   print ("in replace")
   return content

输出如下:

<callable-iterator object at 0x2ab2e09cfe10>
in split
in replace

我用不同的字符串尝试了算法,它工作正常.我还测试了内容是否是字符串,并且是.为什么程序即使进入 split() 也不进入 for..loop?

I have tried the algorithm with a different string and it works fine. I have also tested to see if content is a string and it is. Why isn't the program going into the for..loop even though it is going into split()?

谢谢.

推荐答案

查看评论:

def split(content):
   pattern = re.compile(r"""(\\\[-16pt]\n)(.*?)(\n\\\nthinhline)""", re.X | re.DOTALL)

   # the message you're seeing is correct - this line prints an iterator object -
   # like all iterators, you must actually iterate over it to see the iterator's
   # contents. You're seeing the string representation of an iterator, not the
   # iterator's contents.
   print(pattern.finditer(content))

   # this will iterate over the match objects in the iterator object - but there
   # is no guarantee that any exist
   for m in pattern.finditer(content):
       print ("in for loop")
       print("Matched:\n----\n%s\n----\n" % m.group(2))

   # now you're printing this string, which you correctly observed - note that it is
   # outside of the for loop. This means that its execution is not dependent on the 
   # regex actually finding any matches.
   print ("in split")

由于从未打印过in for loop",这意味着您的正则表达式从未匹配过.我使用 Python 正则表达式工具网站调试我的正则表达式取得了很大的成功.尝试在一些示例文本上使用该网站,以确保您的正则表达式实际上与您期望的位置相匹配.

Since "in for loop" was never printed, that means that your regex never matched. I've had good success using the Python Regex Tool website to debug my regexes. Try using that website on some sample text to make sure your regex actually matches where you expect it to.

您当前的问题只是您的正则表达式没有找到任何匹配项.

Your current problem is simply that your regex isn't finding any matches.

这篇关于重新编译与我的字符串不匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆