我如何使用reg.compile匹配文本的确切单词

时间:2019-06-03 16:27:40

标签: python regex

我想为这样的确切单词找到一种模式(RE.COMPILE)

想象一下这样的词[以太,海拔,阿弗里隆,西]

使用哪种捕获词或带有标点符号的词,以一种可以使我零用地使用的方式,我使用了它,但是它不起作用

% python3 test.py
['(a>(b>c))', '((a>b)>c)']
%

如果正则表达式捕获“单词”和“单词”(单词+标点符号),那就太好了 就像这句话

“西部,可以看看”

结果应该是

西部

3 个答案:

答案 0 :(得分:1)

如果我们希望匹配特定的单词,则可能希望以类似于以下内容的表达式开头:

(aether|altitude|aphelion|apside|apsis|ascension|autumnal equinox|east|eastward|eclipse|ecliptic|elliptical|epicycle|equinoctical|exquinox|fixed star|latitude|longitudes?|mean ecliptic|meridian|mobile star|nodes?|north|octant|orbit|\borbital\b|\bparallax\b|\brays\b|\bretrograde\b|rise|sidereal|sidereal position|solstice|south|star|vernal equinox|west),?

Demo 1

,然后通过在char类中添加所需的标点进行修改:

[,:;\.]?

,我们的表情很可能变成:

(aether|altitude|aphelion|apside|apsis|ascension|autumnal equinox|east|eastward|eclipse|ecliptic|elliptical|epicycle|equinoctical|exquinox|fixed star|latitude|longitudes?|mean ecliptic|meridian|mobile star|nodes?|north|octant|orbit|\borbital\b|\bparallax\b|\brays\b|\bretrograde\b|rise|sidereal|sidereal position|solstice|south|star|vernal equinox|west)[,:;\.]?

Demo 2

测试

# coding=utf8
# the above tag defines encoding for this document and is for Python 2.x compatibility

import re

regex = r"(aether|altitude|aphelion|apside|apsis|ascension|autumnal equinox|east|eastward|eclipse|ecliptic|elliptical|epicycle|equinoctical|exquinox|fixed star|latitude|longitudes?|mean ecliptic|meridian|mobile star|nodes?|north|octant|orbit|\borbital\b|\bparallax\b|\brays\b|\bretrograde\b|rise|sidereal|sidereal position|solstice|south|star|vernal equinox|west),?"

test_str = ("aether\n"
    "altitude\n"
    "aphelion\n"
    "apside\n"
    "apsis\n"
    "ascension\n"
    "autumnal equinox\n"
    "east?.\n"
    "eastward\n"
    "eclipse\n"
    "ecliptic\n"
    "elliptical\n"
    "epicycle\n"
    "equinoctical\n"
    "exquinox\n"
    "fixed star\n"
    "latitude\n"
    "longitude\n"
    "longitudes\n"
    "mean ecliptic\n"
    "meridian\n"
    "mobile star\n"
    "node\n"
    "nodes\n"
    "north\n"
    "octant\n"
    "orbit\n"
    "orbital\n"
    "parallax\n"
    "rays\n"
    "retrograde\n"
    "rise\n"
    "sidereal\n"
    "sidereal position\n"
    "solstice\n"
    "south\n"
    "star\n"
    "vernal equinox\n"
    "west")

matches = re.finditer(regex, test_str, re.MULTILINE)

for matchNum, match in enumerate(matches, start=1):

    print ("Match {matchNum} was found at {start}-{end}: {match}".format(matchNum = matchNum, start = match.start(), end = match.end(), match = match.group()))

    for groupNum in range(0, len(match.groups())):
        groupNum = groupNum + 1

        print ("Group {groupNum} found at {start}-{end}: {group}".format(groupNum = groupNum, start = match.start(groupNum), end = match.end(groupNum), group = match.group(groupNum)))

# Note: for Python 2.7 compatibility, use ur"" to prefix the regex and u"" to prefix the test string and substitution.

答案 1 :(得分:1)

label = "PLAN"
texts = texts 
regex_patterns = [
re.compile(r'(\bAldebaran\b|\bAlphard\b|\bAntares\b|\bArcturus\b|\bBack of Leo\b|\bBeta Leonis\b|\bBeta Scorpii\b|\bBeta Tauri\b|\bBetelgeuse\b|\bcanis\b|\bCanis Minor\b|\bCor Leonis,7\b|\bCor Leonis\b|\bCor Scorpii,10\b|\bCor Scorpii\b|\bDenebola\b|\bdog\b|\bEpsilon Virginis\b|\bErichthonius\b|\bAldebaran\b|\bAlphard|\bAntares\b|\bArcturus\b|\bBack of Leo\b|\bBeta Leonis\b|\bBeta Scorpii\b|\b\Beta Tauri\b|\bBetelgeuse\b|\bcanis\b|\bCanis Minor\b|\bCor Leonis,7\b|\bCor Leonis\b|\bCor Scorpii\b|\bDenebola\b|\bdog\b|\bEpsilon Virginis\b|\bErichthonius\b|\bDenebola\b|\bdog\b|\bEpsilon Virginis\b|\bErichthonius\b|\bHeart of Hydra,8|\bHeart of Hydra\b|\bHydrae\b|\bKappa Geminorum\b|\bLambda Leonis\b|\bNeck of Leo\b|\bOrion\b|\bPalilicium\b|\bPolaris\b|\bPollux\b|\bProcyon\b|\bRegulus\b|\bSpica Virginis\b|\bTail of Leo\b|\bUrsa\b|\bUrsa Major\b|\bVindemiatrix\b|\bZeta Leonis\b)[:,]?')
]

当我遇到双标点(或不一致)时,我捕获了所有像这样的单词:

\bCor Scorpii,10

答案 2 :(得分:0)

尝试此正则表达式:

'(word|other|foo|bar)+[\,\.]?'
匹配 wordword,foofoo,以及带有和不带有标点符号{{1} },,或您添加的其他内容。