Regex First Digits Occurrence

Regex to match only the first occurrence of four numbers in a line

use this :

(^|\.)\s*([0-9]{4})\s*(\.|$)

DEMO

Regex first digits occurrence

You could match 34 (the first digits after VSCA) using a positive lookbehind (?<=VSCA\D*) to assert that what is on the left side is VSCA followed by zero or times not a digit \D* and then match one or more digits \d+:

(?<=VSCA\D*)\d+

If you need the pipe to be after VSCA the you could include that in the lookbehind:

(?<=VSCA\|)\d+

Demo

Regex to find the first occurence of first 8 digits from a string in Java

This should get you what you want:

^([0-9]{8}).*
  • ^ : matches the beginning of the line
  • ([0-9]{8}) : matches and captures the first 8 numeric digits
  • .* : matches the rest of the string and does not capture it. (you
    could probably leave this part off)

Match only first occurrence of digit

With GNU grep:

nmcli --version | grep -Po ' \K[[:digit:]]'

Output:


1

See: Support of \K in regex

Split String at First Occurrence of an Integer using R

You can use tidyr::extract:

library(tidyr)
df <- df %>%
extract("name_and_address", c("name", "address"), "(\\D*)(\\d.*)")
## => df
## name address
## 1 Mr. Smith 12 Some street
## 2 Mr. Jones 345 Another street
## 3 Mr. Anderson 6 A different street

The (\D*)(\d.*) regex matches the following:

  • (\D*) - Group 1: any zero or more non-digit chars
  • (\d.*) - Group 2: a digit and then any zero or more chars as many as possible.

Another solution with stringr::str_split is also possible:

str_split(df$name_and_address, "(?=\\d)", n=2)
## => [[1]]
## [1] "Mr. Smith" "12 Some street"

## [[2]]
## [1] "Mr. Jones" "345 Another street"

## [[3]]
## [1] "Mr. Anderson" "6 A different street"

The (?=\d) positive lookahead finds a location before a digit, and n=2 tells stringr::str_split to only split into 2 chunks max.

Base R approach that does not return anything if there is no digit in the string:

df = data.frame(name_and_address = c("Mr. Smith12 Some street", "Mr. Jones345 Another street", "Mr. Anderson6 A different street", "1 digit is at the start", "No digits, sorry."))

df$name <- sub("^(?:(\\D*)\\d.*|.+)", "\\1", df$name_and_address)
df$address <- sub("^\\D*(\\d.*)?", "\\1", df$name_and_address)
df$name
# => [1] "Mr. Smith" "Mr. Jones" "Mr. Anderson" "" ""
df$address
# => [1] "12 Some street" "345 Another street"
# [3] "6 A different street" "1 digit is at the start" ""

See an online R demo. This also supports cases when the first digit is the first char in the string.

How to split a string in two by first digit occurrence?

There's 3 things going on here.

  1. String.split usually doesn't includes the matched delimiter in the return array. So splitting abc.split('b') would return ['a', 'c']. This behavior can be changed by using a matching regex group; i.e. adding parens 'abc'.split(/(b)/) will return ['a', 'b', 'c'].

  2. String.split will keep the delimter seperate from the other elements. I.e. 'abc'.split(/(b)/) will return 3 elements ['a', 'b', 'c']. Suffix the regex with .* to combine the last 2 elements: 'abc'.split(/(b.*)/) will return ['a', 'bc', ''].

  3. Lastly, to ignore the last empty element, we send the 2nd param of 2.