正则表达式匹配技术问询:如何匹配文本中的'chapter'与'part'并排除衍生词
Got it, let's tackle this regex problem step by step. Your goal is to match content containing chapter or part, but exclude words where part is just a prefix (like party, particular), while still catching cases like part1, part 1, part: hello world, and standalone part.
First, let's break down why your previous attempts fell short:
- The regex with
[\s,:,0-9]+fails for standalonepart(like inhello world part) because it requires at least one character afterpart/chapter. - Using
\bpart\bworks for standalonepart, but doesn't capture trailing content (like the: 1inpart: 1), and it won't matchpart1—since digits count as word characters, there's no word boundary betweentand1.
The Solution Regex
Here's a regex that hits all your requirements:
import re pattern = r'\b(?:chapter|part(?!\p{L}))(?:[\s,:].*)?'
Let's break down each component:
\b: Word boundary, ensures we're matchingpart/chapteras distinct terms (no extra leading letters, like inxpart).(?:chapter|part(?!\p{L})): A non-capturing group to target valid terms:chapter: No restrictions here since you didn't mention excluding its derivatives.part(?!\p{L}): Matchespartonly if it's not followed by a letter (\p{L}matches any Unicode letter). This directly excludes derivatives likeparty,particular, andparticipant.
(?:[\s,:].*)?: Optional non-capturing group to grab trailing content:[\s,:]: Matches common separators (space, colon, comma)—adjust this set if you need to include other characters like hyphens..*: Matches all remaining content until the end of the line (use.*?instead for non-greedy matching if you have multiple matches in one string).?: Makes this entire group optional, so standalonepart/chapterare still matched.
Testing Against Your Samples
Let's verify with your test strings:
s1='part1': Matches'part1's2='part 1': Matches'part 1's3='part: 1': Matches'part: 1's4='part hello world': Matches'part hello world's5='part: hello world': Matches'part: hello world's6='hello world part': Matches'part'
And it correctly ignores derivatives like party, particular, and participant.
内容的提问来源于stack exchange,提问作者Cihan Senol
相关产品推荐
相关产品推荐

