如何用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:- The
[^\)]at the end required a character after the closing parenthesis, so it missed pauses at the end of strings (like(3.2)). - The
.*?was too vague—it would match any characters inside the parentheses, not just the valid pause formats.
- The
内容的提问来源于stack exchange,提问作者Chris Ruehlemann
相关产品推荐
相关产品推荐

