python中等效的正则表达式是什么?

PHP中的代码


<?php

    $str = "CSIR-National Botanical Research Institute, Plant Transgenic Laboratory, U.P., India. Electronic address: i.sanyal@nbri.res.in.";

    preg_match("/([A-Z][^\s,.]+[.]?\s[(]?)*(Hospital|University|Institute|Law School|School of|Academy|College)[^,\d]*(?=,|\d)/", $str, $org_arr);

    echo $org_arr[0];   

?>

输出


CSIR-国家植物研究所


此正则表达式从给定的 PHP 字符串中提取医院、大学、研究所、学校、学院或学院。我尝试在 python 中执行相同的正则表达式,但它不起作用。


Python 中的代码


import re

line = "CSIR-National Botanical Research Institute, Plant Transgenic Laboratory, U.P., India. Electronic address: i.sanyal@nbri.res.in."

match = re.search(r'/([A-Z][^\s,.]+[.]?\s[(]?)*(Hospital|University|Institute|Law School|School of|Academy|College)[^,\d]*(?=,|\d)/', line)

print(match.group(0))

给出错误信息


回溯(最近一次调用最后一次):文件“C:\Users\Ghost Rider\Documents\Python\temp.py”,第 4 行,在 print(match.group(0)) AttributeError: 'NoneType' object has no attribute '团体'


四季花海
浏览 149回答 1
1回答

精慕HU

编辑:不错的附加细节。您在 None 类型上遇到错误,因为该模式不匹配任何内容;展示如何检查比解释更容易......所以让我们稍微改变一下你的例子,看看这是否符合你的要求。请注意模式上缺少前导和尾随斜线(请参阅下面的原文)。import retxt = "CSIR-National Botanical Research Institute, Plant Transgenic Laboratory, U.P., India. Electronic address: i.sanyal@nbri.res.in."# note: str is the string class type, python would happily let you assign that to a string literal.print('txt={}'.format(txt))pattern = r'([A-Z][^\s,.]+[.]?\s[(]?)*(Hospital|University|Institute|Law School|School of|Academy|College)[^,\d]*(?=,|\d)'m = re.search(pattern, txt)if m:&nbsp; &nbsp; print('found some things, groups={}'.format(m.groups()))else:&nbsp; &nbsp; print('no match')结果:txt=CSIR-National Botanical Research Institute, Plant Transgenic Laboratory, U.P., India. Electronic address: i.sanyal@nbri.res.in.found some things, groups=('Research ', 'Institute')我认为PHP 中的$org_arr部分是在 Python 的m.groups()列表中设置的。原来的:也许在没有前导和尾随斜杠的情况下在 python 中尝试一下?让我们从制作一个简单的模式开始......PHP 示例这些PHP 文档显示了这个例子:// The "i" after the pattern delimiter indicates a case-insensitive searchif (preg_match("/php/i", "PHP is the web scripting language of choice.")) {&nbsp; &nbsp; echo "A match was found.";} else {&nbsp; &nbsp; echo "A match was not found.";}由于他们只是在php上搜索,所以斜线看起来像模式分隔符。python中的相同示例在 Python 中就是这样(不是模式是 r'php',不是 r'/php/')。import reif re.match( r'php', 'PHP is the web scripting language of choice.', re.IGNORECASE):&nbsp; &nbsp; print('A match was found.')else:&nbsp; &nbsp; print('A match was not found.')保留匹配对象稍微有用一点,这样你就可以使用你的组......import rem = re.match( r'(php)', 'PHP is the web scripting language of choice.', re.IGNORECASE)if m:&nbsp; &nbsp; print('A match was found, group(1)={}'.format(m.group(1)))else:&nbsp; &nbsp; print('A match was not found.')
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python