繁体   English   中英

如何在 python 中使用正则表达式从段落中提取连字符或星号之间的句子

[英]How to extract a sentence between hyphen or asterisk from a paragraph using regex in python

import re
line="Hello world -- sam -- , How are you? what are *you* doing?"
pattern=r"(?<=\-|\*)(.*?)(?=\-\*)"
print(re.findall(pattern,line))

我得到的 output 是“无”。 帮我解释一下——我应该使用哪种模式,这样我才能得到这个 output:

sam
you

你在找这个吗?

 /[-]{2}\s*(.*?)[-]{2}\s*|[\*]{1}\s*(.*?)[\*]{1}\s*/gm

捕获组 1。

这是预览https://regex101.com/r/ms1dxy/5

细节:

1st Alternative [-]{2}\s*(.*?)[-]{2}\s*

[-]{2} match character - exactly 2 times.

\s* matches any whitespace character (equal to [\r\n\t\f\v ]) between zero and unlimited times

1st Capturing Group (.*?)

.*? matches any character (except for line terminators) between zero and unlimited times

[-]{2} match character - exactly 2 times.

\s* matches any whitespace character (equal to [\r\n\t\f\v ]) between zero and unlimited times


2nd Alternative [\*]{1}\s*(.*?)[\*]{1}\s*

[\*]{1} match character * exactly 1 time.

\s* matches any whitespace character (equal to [\r\n\t\f\v ]) between zero and unlimited times

1st Capturing Group (.*?)

.*? matches any character (except for line terminators) between zero and unlimited times

[\*]{1} match character * exactly 1 time.

\s* matches any whitespace character (equal to [\r\n\t\f\v ]) between zero and unlimited times

您的问题对正则表达式的约束没有足够的了解,无法获得正确的答案。 但是,如果这个 ( RegEx ) 对你来说是新的,那似乎很好。 我(实际上)想说的是:

起作用:

((?:--[\w\s]+--)|(?:\*[\w\s]+\*))

在这一个中, token“定界符”之间允许有任意/未指定数量的空格。

...但是这个RegEx也可以工作 - 它会匹配String's不同子集(包括您在问题中提供的子集):

((?:-- \w+ --)|(?:\*\w+\*))

RegEx精确匹配您在示例中提供的空格数,但会拒绝您可能想到的其他匹配项。 这是所问问题中示例的不明确部分。 下面,标记将与上面的表达式不匹配(它们都不匹配):

 "How are you * doing * today?" "Do you think --Regular Expressions-- are useful to programmers?" "This particular -- #token3 -- has a non-word symbol in it"

这个 Regular-Expression 可能是最“包罗万象”的解决方案,但也许您不需要匹配不含单词Tokens

((?:--[^-\n]+--)|(?:\*[^\*\n]+\*))

此正则表达式将匹配任何文本作为令牌- 包含换行符\n或指定分隔符*-的文本除外。 例如,阅读以下示例:

 "This example -- token has spaces and the $ symbol -- This does match," "This one *here-has-a-few-dashes*. which suits this regex just fine." "This example --misses-completely-- because the token contains the delimiter!"

简而言之,就 python 的正则表达式而言,可能已经发布了数十种变体,所有这些变体都可以解决该问题中提到的一个示例。 此外,可能还需要使用其他后(后)reg-ex 匹配处理。 例如,您可能需要 String 的trim() function 或 String replace ……我个人无法分辨。 坚持下去。

您不会消耗所有连续的左右上下文。 这是环顾四周的错误使用。

采用

[-*]+\s*([^\s*-].*?)\s*[-*]+

证明

解释

--------------------------------------------------------------------------------
  [-*]+                    any character of: '-', '*' (1 or more
                           times (matching the most amount possible))
--------------------------------------------------------------------------------
  \s*                      whitespace (\n, \r, \t, \f, and " ") (0 or
                           more times (matching the most amount
                           possible))
--------------------------------------------------------------------------------
  (                        group and capture to \1:
--------------------------------------------------------------------------------
    [^\s*-]                  any character except: whitespace (\n,
                             \r, \t, \f, and " "), '*', '-'
--------------------------------------------------------------------------------
    .*?                      any character except \n (0 or more times
                             (matching the least amount possible))
--------------------------------------------------------------------------------
  )                        end of \1
--------------------------------------------------------------------------------
  \s*                      whitespace (\n, \r, \t, \f, and " ") (0 or
                           more times (matching the most amount
                           possible))

Python 代码

import re
line="Hello world -- sam -- , How are you? what are *you* doing?"
pattern=r"[-*]+\s*([^\s*-].*?)\s*[-*]+"
print(re.findall(pattern,line))

结果:

['sam', 'you']

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM