正则表达式代码

regular expression

Tool
R
Python
Author

Tony D

Published

March 25, 2025

Top useful Linux command regular expression

R

view

Code
library(stringr)
Code
library(stringr)
pattern='cat'
str_view_all("The cat sat on the mat with another cat.", pattern)

Extract all numbers from a string:

Code
pattern="\\d+"
str_view("I bought 3 apples, 12 bananas, and 5 oranges.",pattern)
Code
str_extract_all("I bought 3 apples, 12 bananas, and 5 oranges.", pattern) |> unlist()

Extract string between two string:

Code
a <- "STR1 11111 STR2 STR1 22222 STR2,"
res <- str_extract_all(a, "STR1\\s*(.*?)\\s*STR2")
res

a=res |> unlist()

print(paste0("first match: ",a[1]))
      
print(paste0("second match: ",a[2]))

get 4 length number and 2 length string

  • \b: Asserts a word boundary position, ensuring that the match is at the beginning or end of a word.

  • \d{4}: Matches exactly four digits.

  • [a-zA-Z]{2}: Matches exactly two letters (uppercase or lowercase).

  • \b: Asserts another word boundary to ensure the ends at a word boundary.

Code
pattern="\\b\\d{4}[a-zA-Z]{2}\\b"
str_view_all("The 1234cat sat on the 1245ma with another 4444ee cat.", pattern)
Code
pattern="\\b\\d{4}[a-zA-Z]{2}\\b"
matches <- str_extract_all("The 1234cat sat on the 1245ma with another 4444ee cat.",pattern)
matches

match sperical

regular expression for getting string between ‘/’ and ’\(' on "The /1234cat\) sat on the 1245ma with another 4444ee cat.”

Code
pattern= "/(.*?)\\$"
str_view_all("The /1234cat$ sat on the 1245ma with another 4444ee cat.", pattern)
Code
pattern= "/(.*?)\\$"
matches <- str_extract_all("The /1234cat$ sat on the 1245ma with another 4444ee cat.",pattern)
matches

Python