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.
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']
.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', '']
.Lastly, to ignore the last empty element, we send the 2nd param of
2
.
let str = "word word 12 otherword(s) 2000 19";
let splitStr = str.split(/(\d.*)/, 2);
console.log(splitStr);
Related Topics
How to Combine Values of Several Lists into One in C#
Posting Jsonobject With Httpclient from Web API
Using Formflow Dialog in Bot Framework
How to Have a a Razor Action Link Open in a New Tab
How to Validate a Datetime in C#
Asp.Net Core 2 Web API Timeout Issue
C# Json.Net How to Ignore a Property in Deserialization But Not in Serialization
How to Map to a Dictionary Object from Database Results Using Dapper Dot Net
Epplus: Find If the Entire Row Is Empty in Excel
Httpcontext.Current.Request.Files Is Always Empty
Launch an Application and Send It to Second Monitor
How to Remove Illegal Characters from Path and Filenames
Visual Studio Solution Unavailable (Reload Doesn't Work)
Response to Preflight Request Doesn't Pass Access Control Check (Angular2)