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

如何用R语言正则提取单圆括号停顿并排除双圆括号注释

Solution for Extracting Single-Parenthesis Pauses in Transcriptions

Got it, let's work through this regex problem for your transcription data. The goal is to pull out only the single-parenthesis pause markers (like (.) or (3.2)) while ignoring the double-parenthesis transcriber notes (like ((giggles))), right?

The Targeted Regex Pattern

Here's a precise regex that will correctly match only your pause markers:

\((?!\()(?:\.|\d+(?:\.\d+)?)\)

Let's break down each part to make it clear:

  • \(: Matches the opening single parenthesis
  • (?!\(): A negative lookahead that ensures the next character isn't another opening parenthesis (this filters out double-parenthesis notes entirely)
  • (?:\.|\d+(?:\.\d+)?): A non-capturing group that targets the two valid pause formats:
    • \.: The short pause marker (.)
    • \d+(?:\.\d+)?: A number (either an integer like (3) or a decimal like (0.5))
  • \): Matches the closing single parenthesis

Testing with Your R Data

Let's put this into action with your sample vector using stringr (the most straightforward tool for regex in R):

library(stringr)

# Your original transcription data
yy <- c("well [yes right] (.)", "let's go ((giggles))", "oh [ we::ll] i do n't (0.5) know", "erm [°well right° ]", "(3.2)")

# Extract all valid pause markers
pause_matches <- str_extract_all(yy, "\\((?!\\()(?:\\.|\\d+(?:\\.\\d+)?)\\)")

# View the results
pause_matches

This will output exactly what you need:

[[1]]
[1] "(.)"

[[2]]
character(0)  # No matches here—this is a double-parenthesis note

[[3]]
[1] "(0.5)"

[[4]]
character(0)  # No pauses present in this string

[[5]]
[1] "(3.2)"

If you want all matches in a single flat vector, just use unlist():

unlist(pause_matches)
# Output: [1] "(.)"   "(0.5)" "(3.2)"

Why Your Previous Patterns Failed

  • The first pattern (\(.*?\)) was too broad—it matched any content wrapped in parentheses, whether it was a pause or a transcriber note.
  • The second pattern \([^\(].*?\)[^\)].*? had two critical flaws:
    1. The [^\)] at the end required a character after the closing parenthesis, so it missed pauses at the end of strings (like (3.2)).
    2. The .*? was too vague—it would match any characters inside the parentheses, not just the valid pause formats.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.28 09:22:47