Match letters in R regex


Suppose I run the following

txt <- "client:A, field:foo, category:bar" grep("field:[A-z]+", txt, value = TRUE, perl = TRUE)

Based on <a href="http://regexr.com" rel="nofollow noreferrer">regexr.com</a> I expected I would get field:foo, but instead I get the entire string. Why is this?


You seem to want to extract the value. Use regmatches:

txt <- "client:A, field:foo, category:bar" regmatches(txt, regexpr("field:[[:alpha:]]+", txt)) # => [1] "field:foo"

See the <a href="https://ideone.com/K1vfTC" rel="nofollow">R demo</a>.

To match multiple occurrences, replace regexpr with gregexpr.

Or use <em>stringr</em> str_extract_all:

library(stringr) str_extract_all(text, "field:[a-zA-Z]+")

Another point is that <a href="https://stackoverflow.com/questions/29771901/why-is-this-regex-allowing-a-caret/29771926#29771926" rel="nofollow">[A-z] matches more than ASCII letters</a>. Use [[:alpha:]] in a TRE (regexpr / gregexpr with no perl=TRUE)/ICU (stringr) regex to match any letter.


  • Regular expression to find function calls in a function body
  • Regex Javascript Phone number validation min max length check
  • redirect without changing url
  • Zend route regex, optional parameters
  • URL regex excluding a specific domain not matching correctly
  • Visual Studio 2013 Browser Link not playing well with Firefox - throwing exception
  • azure web job “The job is missing basic auth fields”
  • Extract data between rows r
  • Google Places API - Find a company's CID and LRD
  • perl, mysql - fasting way to upload a csv file into mysql?
  • Get specific string
  • Is there a perl module to validate passwords stored in “{crypt}hashedpassword” “{ssha}hashedpassword
  • Converting a WriteableBitmap image ToArray in UWP
  • PHP buffered output depending on server setting?
  • Functions in global context
  • Use of this Javascript
  • How to create a file in java without a extension
  • C++ Partial template specialization - design simplification
  • Spring Data JPA custom method causing PropertyReferenceException
  • Splitting given String into two variables - php
  • C# - Is there a limit to the size of an httpWebRequest stream?
  • What is Eclipse's Declaration View used for?
  • Counter field in MS Access, how to generate?
  • Is possible to count alias result on mysql
  • Possible to stop flickering java tooltip in heavyweight mode?
  • Check if a string to interpolate provides expected placeholders
  • sending/ receiving email in Java
  • vba code to select only visible cells in specific column except heading
  • How to set my toolbar fixed while scrolling android
  • How to get next/previous record number?
  • R: gsub and capture
  • AT Commands to Send SMS not working in Windows 8.1
  • Python: how to group similar lists together in a list of lists?
  • Windows forms listbox.selecteditem displaying “System.Data.DataRowView” instead of actual value
  • apache spark aggregate function using min value
  • sending mail using smtp is too slow
  • Busy indicator not showing up in wpf window [duplicate]
  • Sorting a 2D array using the second column C++
  • Why is Django giving me: 'first_name' is an invalid keyword argument for this function?
  • How can I use `wmic` in a Windows PE script?