Regex: How to remove FQN [duplicate] - regex

This question already has answers here:
Regex match everything up to first period
(3 answers)
Closed 4 years ago.
I want to remove the domain information from hostnames.
E.g. I want "server1.mydomain.com" to be just "server1".
I thought I had it with:
^(\w*)
But then I realized I also have hostnames like "desktop-1.mydomain.com", and they all got changed to "desktop" and not "desktop-1" etc.
Any suggestions how to do this?

As already mentioned by Wiktor in the comments, the easiest regular expression is
^[^.]+
The explanation from regex101.com is:
^ asserts position at start of a line
Match a single character not present in the list below [^.]+
+ Quantifier — Matches between one and unlimited times, as many times as
possible, giving back as needed (greedy)
. matches the character . literally (case sensitive)
If you are using a programming language, another possible solution is to split the string in the dot character and get the first element of the resulting array. For example:
const array1 = 'server1.mydomain.com'.split(/\./);
console.log(array1[0]);
const array2 = 'desktop-1.mydomain.com'.split(/\./);
console.log(array2[0]);
Prints:
server1
desktop-1

Related

Regex pattern alternative for (negative) lookbehinds of arbitrary, non-zero length [duplicate]

This question already has answers here:
Regular expression for a string containing one word but not another
(5 answers)
Closed 2 years ago.
I lack experience with regex and need help with this problem:
Objective: Create a regex pattern that matches "renal" or "kidney" in an arbitrary string only if it does not contain "carcinoma".
Example strings:
"Renal cell carcinoma"
"Clear cell carcinoma of kidney"
"Chronic renal impairment"
Expected output: The regex pattern does not match "renal" and "kidney" in the first two strings; it does match "renal" in the third string (since there is no "carcinoma").
What I've tried: (?<!carcinoma).*(kidney|renal). I stopped here because it didn't work — because, as I've learned here and here, lookbehinds are limited to basically non-zero length; regular expressions cannot be applied backwards an arbitrary length.
So what regex pattern will do the trick? I want a pattern that maintains focus on (or is "anchored" on) "renal" and "kidney" and not "carcinoma".
The pattern that you tried (?<!carcinoma).*(kidney|renal) asserts what is directly to the left is not carcinoma which is true from the start of the string.
Then it will match any char 0+ times until the end of the string and tries to backtrack to fit in either kidney or renal.
Instead of using (?<!carcinoma), use ^(?!.*\bcarcinoma\b) to assert from the start of the string that bcarcinoma is not present at the right.
Then match either the word renal or kidney in the string.
^(?!.*\bcarcinoma\b).*\b(renal|kidney)\b.*
Regex demo

Regular expression to match ) under certain conditions [duplicate]

This question already has answers here:
Regex for matching something if it is not preceded by something else
(3 answers)
Closed 2 years ago.
I am trying to match ) if there is a ( and two numbers to the left of it.
Example "(55)"
I want to match ) in "(55)"
I do not want to match "(hello world 55)"
I currently have the following as my regex:
\(\d+\)
It matches "(55)" but I just want the ) in it. Is there a way to get a certain character by placement in a regex? Or do you have a better solution?
There are multiple ways to match what you want.
For instance, you can use \K to rest the previous match:
\(\d+\K\)
Using positive lookbehind
(?<=\(\d+)\)
Also capturing the content for the match
\(\d+(\))
Flutter and all Languages supported
Insert all Special Character, Number, Lowercase & Uppercase
condition 1: Must Length of word 8
condition 2: Insert any of above given list (Not Must in all type)
r'^([a-zA-Z].)*.{8,}$' ,
Must Insert Atleast One in Above given list,
r'^(?=.*[A-Za-z])(?=.*\d)[A-Za-z\d]+!##\$&*~{8,}$',

How to do regexp replace/search [duplicate]

This question already has an answer here:
Reference - What does this regex mean?
(1 answer)
Closed 5 years ago.
I am following some instructions for data upload. I can't figure out what the following two points mean. Does anyone have any idea?
Regexp search/replace
search: 201([0-9])([0-9])([0-9])([0-9][0-9]) ([0-9])
replace:201\1\2\3\4 \5
Regexp search/replace
replace 20110401 with whatever year month day that is being fixed
^(.{462})
\120110401
Any decent regex tutorial will help.
() wrap groups that can be referenced later with \#. For example, \2 references the token matched by the second pair of parentheses.
[0-9] means any character between 0-9 inclusive.
^ is the left anchor (i.e., start of string or new line), and .{462} means any character, 462 times.

How regex engine works for "[*=]+$" [duplicate]

This question already has an answer here:
Reference - What does this regex mean?
(1 answer)
Closed 7 years ago.
I'm trying to build regex that removes * and = or any combination of them from the end of the string, so I tried "[*=]$", but it was lazy, for example, if I have the string this is a dog =*, then it will remove * and keep =, then I tried the regex [*=]+$, and it did the job, But I can't understand how the regex engine would work with the last regex, or in another word, how this regex become greedy.
Note that + repeats the previous token one or more times. So [*=]+ matches one or more * or = symbols exists at the last.
What happens in the background is, at first [*=] matches all the * or = symbols (matching continuous characters). Once after regex engine saw the + which exists next to the char class, then it starts to match the following * or = symbols. And finally once it saw the end of the line anchor $, all the matches other than the one exists at the last will get discarded by the regex engine. Now, you left with the last match (match exists at the end of a line).

What does the regular expression ^(\d{1,2})$ mean? [duplicate]

This question already has an answer here:
Reference - What does this regex mean?
(1 answer)
Closed 8 years ago.
I am trying to understand what the regular expression ^(\d{1,2})$ stands for in google sheets. A quick look around the regex sites and in tools left me confused. Can anybody please help?
^ Asserts position at start of the string
( Denotes the start of a capturing group
\d Numerical digit, 0, 1, 2, ... 9. Etc.
{1,2} one to two times.
) You guessed it - Closes the group.
$ Assert position at end of the string
Regular expression visualization:
^ - start of a line.
(\d{1,2}) - captures upto two digits(ie; one or two digits).
$ - End of the line.
It means at least one at most two digits \d{1,2}, no other characters at the beginning ^ or the end $. Parenthesis essentially picks the string in it i.e. what ever the digits are
^ matches the start of the line
The parens can be ignored for now..
\d{1, 2} means one or two digits
$ is the end of the line.
The parens, if you need them, can be used to retrieve the digit(s) that were found in the regex.