You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在R语言中精准提取两个指定单词A与B之间的内容(去除前后空格且仅匹配首个B)

解决提取A和B之间首个匹配内容并去除前后空格的问题

Got it, let's sort out this regex extraction task for you! The key issues with your original code are that the greedy .* matches all the way to the last occurrence of wB, and you're left with unwanted leading/trailing spaces in the extracted content. Here's how to fix it:

核心改进点

  • Use non-greedy matching (.*? instead of .*) to stop at the first wB instead of the last one.
  • Remove leading/trailing spaces from the extracted content, either with a string trimming function or by adjusting the regex to skip those spaces directly.

方案1:非贪婪匹配 + 事后修剪(直观易读)

This approach uses non-greedy regex to capture the content between wA and the first wB, then trims the extra spaces afterward:

library(stringr)

# Define the pattern with non-greedy quantifier
pattern <- "(?<=wA).*?(?=wB)"

# Test with your sample strings
str1 <- "qzpdjpqz wA Hello world ! wB edjifdjiq"
str2 <- "qzpdjpqz wA Hello world ! wB wB"

# Extract and trim the result
result_str1 <- str_trim(str_match(str1, pattern)[, 1])
result_str2 <- str_trim(str_match(str2, pattern)[, 1])

# Check output
result_str1  # Returns "Hello world !"
result_str2  # Also returns "Hello world !" (stops at first wB)

# Handle multi-line text
str11 <- "qzpdjpqz wA word1 wB edjifdjiq\n qzpdjpqz wA word2 wB wB\n qzpdjpqz gregegt wA word3 wB wB\n rsgeef vfsfeqz wA word4 wB "
results_multi <- str_trim(str_extract_all(str11, pattern)[[1]])
results_multi  # Returns ["word1", "word2", "word3", "word4"]

方案2:正则直接捕获无空格内容(一步到位)

If you prefer to handle the spaces directly in the regex, you can capture the core content while skipping leading/trailing whitespace between wA and wB:

pattern2 <- "(?<=wA)\\s*(.*?)\\s*(?=wB)"

# Extract the captured group (group 1) instead of the full match
result_str1_2 <- str_match(str1, pattern2)[, 2]
result_str2_2 <- str_match(str2, pattern2)[, 2]

# For multi-line text
results_multi_2 <- str_match_all(str11, pattern2)[[1]][, 2]

This regex works by:

  • (?<=wA): Positive lookbehind to find wA
  • \\s*: Matches any number of whitespace characters (including none) after wA
  • (.*?): Non-greedy capture of the content you want (group 1)
  • \\s*: Matches any number of whitespace characters before wB
  • (?=wB): Positive lookahead to find the first wB

验证结果

Both methods will give you the clean, trimmed content you're expecting, and they'll stop at the first occurrence of wB instead of matching all the way to the last one.

内容的提问来源于stack exchange,提问作者problème0123

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.30 17:02:31