I need some help with regex to extract specific characters from a given string. Following is the string and required string to extract.
21.4R3.13-EVO -- Required 21.4R3-EVO
24.1R13.13-EVO -- Required 24.1R13-EVO
21.4R2-S1-20190223000107.0-EVO -- Required 21.4R2-EVO
19.4R11-S1-20190223000107.0-EVO -- Required 19.4R11-EVO
I tried following and its matching first half but I couldn't negate the middle section.
[0-9]+.[0-9]+[A-Z][0-9]+
Matching
21.4R3
You can use this regex for search:
^(\d+\.[a-zA-Z\d]+)[-.].*(-[a-zA-Z]+)$
and replace with $1$2.
RegEx Demo
RegEx Breakup:
^: Start
(\d+\.[a-zA-Z\d]+): Match 1+ digits then a dot followed by 1+ alpha numeric character. Capture this in group #1
[-.].*:
(-[a-zA-Z]+): Match last - followed by 1+ letters and capture in group #2
$: End
Related
to validate the text using the regex which allows one space or hyphen start with 97.
regex: ^97(?=[0-9]+[ -]?[0-9]+)(?!([0-9])(?:\1|[ -]){2}).{3}$
ex: 97123 valid
97-12 valid but not working as per the above reg
971-2 valid
9712- not valid
You can use
^(?=[0-9]+(?:[ -][0-9]+)?$)(?!([0-9])(?:\1|[ -]){2})97.{3}$
See the regex demo
The first lookahead, (?=[0-9]+(?:[ -][0-9]+)?$), now requires
[0-9]+ - one or more digits
(?:[ -][0-9]+)? - an optional occurrence of a space or hyphen and then one or more digits
$ - end of string
to be present immediately to the right of the current location (that is the start of string).
Your requirements can be covered with this simplified regex:
^97(?=.{3}$)(?:[ -]?\d)+$
RegEx Demo
RegEx Breakup:
^: Start
97: Match 97
(?=.{3}$): Match 3 more character till end
(?:[ -]?\d)+: Match an optional hyphen or space followed by a digit. Match this non-capturing group 1+ times
$: End
I have the following example of numbers, and I need to add a zero after the second period (.).
1.01.1
1.01.2
1.01.3
1.02.1
I would like them to be:
1.01.01
1.01.02
1.01.03
1.02.01
I have the following so far:
Search:
^([^.])(?:[^.]*\.){2}([^.].*)
Substitution:
0\1
but this returns:
01 only.
I need the 1.01. to be captured in a group as well, but now I'm getting confuddled.
Does anyone know what I am missing?
Thanks!!
You may try this regex replacement with 2 capture groups:
Search:
^(\d+\.\d+)\.([1-9])
Replacement:
\1.0\2
RegEx Demo
RegEx Details:
^: Start
(\d+\.\d+): Match 1+ digits + dot followed by 1+ digits in capture group #1
\.: Match a dot
([1-9]): Match digits 1-9 in capture group #2 (this is to avoid putting 0 before already existing 0)
Replacement: \1.0\2 inserts 0 just before capture group #2
You could try:
^([^.]*\.){2}\K
Replace with 0. See an online demo
^ - Start line anchor.
([^.]*\.){2} - Negated character 0+ times (greedy) followed by a literal dot, matched twice.
\K - Reset starting point of reported match.
EDIT:
Or/And if \K meta escape isn't supported, than see if the following does work:
^((?:[^.]*\.){2})
Replace with ${1}0. See the online demo
^ - Start line anchor.
( - Open 1st capture group;
(?: - Open non-capture group;
`Negated character 0+ times (greedy) followed by a literal dot.
){2} - Close non-capture group and match twice.
) - Close capture group.
Using your pattern, you can use 2 capture groups and prepend the second group with a dot in the replacement like for example \g<1>0\g<2> or ${1}0${2} or $10$2 depending on the language.
^((?:[^.]*\.){2})([^.])
^ Start of string
((?:[^.]*\.){2}) Capture group 1, match 2 times any char except a dot, then match the dot
([^.].*) Capture group 2, match any char except a dot
Regex demo
A more specific pattern could be matching the digits
^(\d+\.\d+\.)(\d)
^ Start of string
(\d+\.\d+\.) Capture group 1, match 2 times 1+ digits and a dot
(\d) Capture group 2, match a digit
Regex demo
For example in JavaScript
const regex = /^(\d+\.\d+\.)(\d)/;
[
"1.01.1",
"1.01.2",
"1.01.3",
"1.02.1",
].forEach(s => console.log(s.replace(regex, "$10$2")));
Obviously, there will be tons of solutions for this, but if this pattern holds (i.e. always the trailing group that is a single digit)... \.(\d)$ => \.0\1 would suffice - to merely insert a 0, you don't need to match the whole thing, only just enough context to uniquely identify the places targeted. In this case, finding all lines ending in a . followed by a single digit is enough.
I am trying to find a regex expression to match strings with 4 repeating digits and optional hyphens in between, such as:
-3-3-3-3-
-1111-
2222-
0-0-00
Currently I have:
\-?(\d(\-*))\1{3}\-?
which matches the first two but not the last two. Any suggestions?
You may use
^-?(\d)(?:-?\1){3}-?$
See the regex demo. To find the pattern in a larger string, remove the ^ and $ anchors:
-?(\d)(?:-?\1){3}-?
If the pattern is a part of a longer pattern, you might have to adjust the backreference number (if there are other capturing groups in the pattern).
Details
^ - start of string
-? - an optional -
(\d) - Group 1: any digit
(?:-?\1){3} - three occurrences of an optional - and then the same value as captured in Group 1
-? - an optional hyphen
$ - end of the string.
Here is my samples string
Google Chrome-Helper -type=renderer -field-trial-handle=1
But I want just Google Chrome-Helper
Ive tried: ^.*[ ][-] but captures up to the last parameter.
Example Here
You need to use lazy dot matching and either use capturing or a lookahead:
^(.*?)\s+-
(your value will be in Group 1) or
^.*?(?=\s+-)
See the regex demo with capturing and with a lookahead.
Details:
^ - start of string anchor
.*? - any 0+ chars other than a newline, as few as possible (i.e. the subsequent subpatterns are tried first, this one is skipped, the regex engine only comes back here if they fail to find a match)
(?=\s+-) - a positive lookahead that requires 1+ whitespace and then a hyphen.
Hello i want to match with regex this word
(Parc Installé)
from this text:
31/1/2017 17:19:23,4245986,ct0001#Intotel.int,Parc Installé,100.100.30.100
I did this regex ',[A-Za-zA-zÀ-ú+ \/\w+0-9._%+-]+,'
But the result is : 4245986 ans Parc Installé.
How can i match only Parc Installé
You may try a regex based on a lookahead that will require a comma and digits/commas after it up to the end of string:
[^,]+(?=\s*,[\d.]+$)
See this regex demo
Details:
[^,]+ - 1 or more chars other than ,
(?=\s*,[\d.]+$) - a lookahead requiring
\s* - zero or more whitespaces
, - a comma
[\d.]+ - 1+ digits or dots up to...
$ - ... the end of string
To make it a bit more restrictive, you may replace the lookahead with (?=\s*,\d+(?:\.\d+){3}$) to require 4 sequences of dot-separated 1+ digits. See this regex demo.
If a lookahead is not supported (case with a RE2 engine), you might want to use a capturing group based solution:
([^,]+)\s*,[\d.]+$
Here, the part within (...) will be captured into Group 1 and will be accessible via a backreference or a function like =REGEXEXTRACT in Google Spreasheets that only retrieves the contents of a capturing group if the latter is present in the pattern.