Match numbers or first letter before a character - regex

Arthur Carvalho Brito

I am trying in R to match a specific pattern to make a separation into columns

Consider these examples of strings:

1-EXAMPLE
23-EXAMPLE2
A-EXAMPLE3
EXAMPLE-4

How can I write a regex to be used in tidyr::extract so that the separation happens as follows:

1   EXAMPLE
23  EXAMPLE2
A   EXAMPLE3
NA  EXAMPLE-4

I want to make a separation at the first - mark if before it there are only numbers, or if there is a single letter beforehand (as in the third case), but not if there more (as in example 4)

Thank you!

akrun

We can use case_when to insert a character before we do extract

library(dplyr)
library(stringr)
library(tidyr)
df1 %>% 
    mutate(col1 = case_when(str_detect(trimws(col1), '^([A-Z]|[0-9]+)\\s*-', 
       negate = TRUE) ~ str_c('-', col1), TRUE ~ trimws(col1))) %>% 
    extract(col1, into = c('col1', 'col2'), '^([A-Z]|\\d+)?\\s*-(.*)') %>% 
    mutate(col1 = na_if(col1, ''))

-output

col1      col2
1    1   EXAMPLE
2   23  EXAMPLE2
3    A  EXAMPLE3
4 <NA> EXAMPLE-4

data

df1 <- structure(list(col1 = c("1-EXAMPLE", "23-EXAMPLE2", "A-EXAMPLE3", 
"EXAMPLE-4")), class = "data.frame", row.names = c(NA, -4L))

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

PHP regex match numbers from string before specified character

what is the regex for first character is a letter?

match first character in a regex?

C# Regex, match but not include the first character before matched string

Regex add a space between words that ends before the letter S with succeeding numbers or before the character <

Regex - Match string before a character

Regex to match before and after a character

Regex, get all character before the second letter

Regex to match a-z spaces, character limit, only first letter capitalized

python regex: match the dot only, not the letter before it

Ignore First Character in Regex Match

Regex to match x numbers and 1 letter

Regex/PHP Remove everything before first letter

Searching for a Regex to disable any "/" before the first letter

regex match the first 3 numbers

RegEx - match where second letter is not first letter and third letter is first letter

regex match as long as it comes before an optional character

Regex match string before and after a specific character

Python Regex match character before or after slash

Regex match character before and after underscore

Regex to match numbers followed by a specific character

Regex to not match numbers followed by a certain character

Regex match a sequence of numbers and a character . between them

Regex - Match all numbers after a character

JavaScript RegEx - First 2 characters letter, rest numbers OR just numbers

Regex match the last group with @ character on the first character match

Regex get all before first occurrence of character

Regex match after numbers before double underscore

RegEx to find if string has first a character and then numbers

TOP Ranking

HotTag

Archive