python - 在 while 循环中输出 re.search 和 re.match 时遇到问题

标签 python regex search match regex-group

我正在尝试使用 python 打印并最终将信息和数据写入文件。我将文件读入名为 List[] 的数组,然后使用正则表达式搜索每一行。我想对文件中的每一行执行此操作,但是当我执行此操作时

counter=0
while counter <= OrderNum:    
    Info=re.match('^.{0,12}',List[counter])
    Data=re.search('=....................',List[counter])
    print counter
    print 'is less than'
    print OrderNum
    print Info.group(counter)
    print Data.group(counter)
    counter+=1

第一次迭代 [0] 一切都很好,但有一次

Output
1
is less than 
4

我收到以下错误

Traceback (most recent call last):
File "C:\Users\*******\workspace\Jython\regularexpressions.py", line 35, in <module>
print Info.group(counter)
IndexError: no such group

最佳答案

match.group(n) 返回正则表达式组 n(如果 n=0,则返回整个匹配项)。正则表达式中的组是括号内的内容,并且您会得到异常,因为您的表达式中没有任何组。向我们展示您尝试匹配的数据,我们将帮助您提供更好的表达方式。

关于python - 在 while 循环中输出 re.search 和 re.match 时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19486706/

相关文章:

jquery - 正则表达式检查密码是否至少包含一个大写字母

javascript - 如何在 JavaScript 中过滤这些搜索结果?

search - 如何访问 NetSuite 自定义列表中自定义列表的 'Abbreviation' 字段

Python pywin32 在其目录中运行应用程序

Python tkinter : How can I ensure only ONE child window is created onclick and not a new window every time the button is clicked?

c++ - 对于复杂的问题解决练习(例如图形),哪种语言(C++ 或 Python)更好?

regex - 使用 RegEx 提取逗号分隔值

python速度处理每行VS block

Java 替换 - 多项选择

c++ - 在c++中的特定文本行之间读取文本文件