You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

如何用Python re模块从混合聊天字符串提取指定用户聊天内容

Extracting Specific User Messages from Continuous Chat Strings in Python

Got it, let's tackle this problem together! Extracting a specific user's messages from a continuous string (no line breaks or punctuation to separate chats) is totally doable with Python's re module—here's how to make it work, even when the same user sends multiple consecutive messages.

First, Let's Define the Problem with an Example

Suppose we have this messy input string where chats are only separated by usernames:

input_str = "JoeHi thereMikeHey Joe, how's it going?JoePretty good, thanks!MikeThat's great to hear.JoeYeah, just finished a project."

Our goal is to strip out all of Mike's messages and his username tag, leaving only Joe's content (we can choose to keep Joe's tags or remove them too).

Method 1: Remove the Unwanted User's Content (Keep Target User's Tags)

The key here is to use a regex that matches the unwanted username (Mike) and everything that follows it—until it hits the target username (Joe) or the end of the string. We'll use re.sub() to replace those matches with nothing.

Here's the code:

import re

input_str = "JoeHi thereMikeHey Joe, how's it going?JoePretty good, thanks!MikeThat's great to hear.JoeYeah, just finished a project."

# Regex breakdown:
# - `Mike`: Matches the unwanted username tag
# - `.*?`: Non-greedy match for any characters (stops at the next boundary)
# - `(?=Joe|$)`: Positive lookahead to stop at either the next Joe tag or the end of the string
cleaned_with_tags = re.sub(r'Mike.*?(?=Joe|$)', '', input_str)

print(cleaned_with_tags)
# Output: "JoeHi thereJoePretty good, thanks!JoeYeah, just finished a project."

Method 2: Extract Only the Target User's Message Content (No Tags)

If you don't want to keep Joe's username tags either, you can either modify the first method to strip them, or use re.findall() to directly pull out the message content after each Joe tag.

Option A: Strip Tags After Cleaning

# Take the cleaned string from Method 1 and remove all Joe tags
content_only = re.sub(r'Joe', '', cleaned_with_tags)
print(content_only)
# Output: "Hi therePretty good, thanks!Yeah, just finished a project."

Option B: Directly Extract Content with findall()

This regex matches everything that comes after a Joe tag, up until the next Mike tag or the end of the string:

# Find all content after "Joe" that stops at "Mike" or the end of the string
message_contents = re.findall(r'Joe(.*?)(?=Mike|$)', input_str)
content_only = ''.join(message_contents)

print(content_only)
# Same output as above: "Hi therePretty good, thanks!Yeah, just finished a project."

Handling Consecutive Messages from the Target User

Both methods work seamlessly even when the target user sends multiple messages in a row. For example, if your input is:

input_str = "JoeFirst messageJoeSecond messageMikeGot it, cool!JoeThird message."

Running the code will correctly leave you with:

  • With tags: JoeFirst messageJoeSecond messageJoeThird message.
  • Without tags: First messageSecond messageThird message.

Edge Cases to Note

  • If the string ends with the unwanted user's message (e.g., JoeHi thereMikeBye), the regex will still remove the entire MikeBye segment.
  • This assumes only two users are present. If there are more users, you can adjust the lookahead to include all other usernames (e.g., (?=Joe|Sara|$)).

内容的提问来源于stack exchange,提问作者Agrosel

火山引擎 最新活动