You can use gregexpr
gregexpr(pattern ='2',"the2quickbrownfoxeswere2tired") [[1]] [1] 4 24 attr(,"match.length") [1] 1 1 attr(,"useBytes") [1] TRUE
or perhaps str_locate_all
from package stringr
which is a wrapper for gregexpr
stringi::stri_locate_all
(as of stringr
version 1.0)
library(stringr) str_locate_all(pattern ='2', "the2quickbrownfoxeswere2tired") [[1]] start end [1,] 4 4 [2,] 24 24
note that you could simply use stringi
library(stringi) stri_locate_all(pattern = '2', "the2quickbrownfoxeswere2tired", fixed = TRUE)
Another option in base R
would be something like
lapply(strsplit(x, ''), function(x) which(x == '2'))
should work (given a character vector x
)
Related Posts:
- OR condition in Regex
- Using or ‘|’ in regex [duplicate]
- What does \’.- mean in a Regular Expression
- How can I validate an email address using a regular expression?
- Difference between \b and \B in regex
- Regex: ignore case sensitivity
- What does the regex \S mean in JavaScript?
- How can I validate an email address using a regular expression?
- Regex: ignore case sensitivity
- Python regex AttributeError: ‘NoneType’ object has no attribute ‘group’
- What is the difference between the regular expressions [^\d\s] and [\D\S]
- grep –ignore-case –only
- Regex AND operator
- python re.split() to split by spaces, commas, and periods, but not in cases like 1,000 or 1.50
- python re.split() to split by spaces, commas, and periods, but not in cases like 1,000 or 1.50
- Regex to test if string begins with http:// or https://
- Regular expression negative lookahead
- Regular Expression to match string starting with a specific word
- Regular Expressions: Is there an AND operator?
- Regular Expression.how to add optional character at end of regex
- How to validate an email address in PHP
- Regular expression to match standard 10 digit phone number
- How to use Regular Expressions (Regex) in Microsoft Excel both in-cell and loops
- Python string.replace regular expression
- How to use Regular Expressions (Regex) in Microsoft Excel both in-cell and loops
- Python string.replace regular expression
- Symbol for any number of any characters in regex?
- Grep and Python
- Regex – Does not contain certain Characters
- How to replace ” \ ” with ” \\ ” in java
- A regular expression to exclude a word/string
- How to validate an email address in JavaScript
- Using Bash regex match (=~) where regex includes quotes (” characters)
- Searching for UUIDs in text with regex
- Regular expression to match a word or its prefix
- How can I remove punctuation from input text in Java?
- Regular expression for exact match of a string
- Using the star sign in grep
- Regex optional group
- Regex for string contains?
- Question marks in regular expressions
- Difference between \w and \b regular expression meta characters
- Regular Expression – Validate Gmail addresses
- Java; String replace (using regular expressions)?
- How can I remove punctuation from input text in Java?
- How can I output only captured groups with sed?
- How to negate specific word in regex?
- What is the grep equivalent in Python?
- javascript HTML from document.body.innerHTML
- How can I write a regex which matches non greedy?
- Regex Explanation ^.*$ [duplicate]
- Negative matching using grep (match lines that do not contain foo)
- How to exclude a specific string constant?
- How to replace a whole line with sed?
- Regex match everything after question mark?
- Validating IPv4 addresses with regexp
- How to validate phone number using PHP?
- Validate phone number with JavaScript
- Test if characters are in a string
- How is \\n and \\\n interpreted by the expanded regular expression?
- Regex to match only uppercase “words” with some exceptions
- Regular Expression to reformat a US phone number in Javascript
- Regex to match only letters
- Finding the complement of a DFA?
- RegEx to find two or more consecutive chars
- Regex to match an optional ‘+’ symbol followed by any number of digits
- Regex Match all characters between two strings
- Regular expression to match a word or its prefix
- Regular expressions in C: examples?
- How to ignore whitespace in a regular expression subject string?
- Regular expression for a string that does not start with a sequence
- python regular expression “\1”
- Regular expression – starting and ending with a character string
- Regular expression to stop at first match
- /bb|[^b]{2}/ how does it work?
- How do I match any character across multiple lines in a regular expression?
- Regular expression to remove HTML tags from a string
- Regular expression to limit number of characters to 10
- Regex that matches integers in between whitespace or start/end of string only
- What is the difference between square brackets and parentheses in a regex?
- My Vim replace with a regex is throwing a `E488: Trailing characters`
- Regular expression that matches valid IPv6 addresses
- Escaping Parentheses in Regex
- How to replace a string in an existing file in Perl
- What is the best regular expression to check if a string is a valid URL?
- Regex empty string or email
- Regex to validate date formats dd/mm/YYYY, dd-mm-YYYY, dd.mm.YYYY, dd mmm YYYY, dd-mmm-YYYY, dd/mmm/YYYY, dd.mmm.YYYY with Leap Year Support
- Wrap element with in any number of elements with PHP
- extract shortcodes from string
- preg_replace and comment_form_defaults
- 404 regular-expression advice needed
- How to use a variable as a className inside preg_match_all parameters?
- How to set up redirects for category paginated pages after adding /category/ to permalink
- Adding span tags to post titles using regex
- Redirect non www to www using redirection plugin regex
- Restrict comments with regular expression
- Remove Custom HTML Posts Using Regex
- Regexes allow only [a-z][A-Z][0-9] and –
- How to fix old embeded iframes with WP-CLI search-replace
- Use Regex Redirection via htsaccess for RSS Feeds WordPress