Does not match when the string does not have a dot but it will match multiple dots [duplicate] - regex

This question already has answers here:
Regex to allow alphanumeric and dot
(3 answers)
Closed 4 years ago.
I am trying to match the string when there's 0 or multiple dots. The regex that I can only match multiple dots but not 0 dot.
(\w*)((\w*\.)+\w*)
These are the test string I am using
dial.check.Catch.Url
dial.check.Catch.Url.Dial.check.Catch.Url
32443.324342.23423424.23.423.423.42.34.234.32.4..2..2.342.4
234dfasfd2aa4234234.234aa341.4.123daaadf.df.af....
12fd.dafd
.
abc
The Regex will match these
dial.check.Catch.Url
dial.check.Catch.Url.Dial.check.Catch.Url
32443.324342.23423424.23.423.423.42.34.234.32.4..2..2.342.4
234dfasfd2aa4234234.234aa341.4.123daaadf.df.af....
12fd.dafd
.
But not this one:
abc
https://regexr.com/?38ed7

If you really must use a regex, here is one (but it is inefficient):
/^(?![^.]*\.[^.]*$).*$/
It says:
Match a string so that the beginning of the string is not followed by a whole string with a single dot.
It does some backtracking when parsing the negative lookahead.
As mentioned in the comments to the question, I do think, unless you must have a regex, that a simple function might be better. But if you like the conciseness of a regex and performance is not a huge concern, you can go with the one I gave above. Regexes with "nots" in them are generally a tad messy, but once you understand lookarounds they do become doable. Cheers.

/\..*\.|^[^.]*$/
Or, in plain English:
Match EITHER a dot, then any number of characters, then another dot; OR the beginning of the string, then any number of non-dots, then the end of the string.

Related

Match character and any whitespaces around it [duplicate]

This question already has answers here:
Splitting a String by number of delimiters
(2 answers)
Closed 2 years ago.
I have a file containing informations in the following format :
Fred,Frank , Marcel Godwin , Marion,Ryan
I need the match commas and any whitespace around them, but not any comma inside brackets.
My problem is that with my current regex [\s,]+ the whitespaces between words are matched. So in this example the whitespace between Marcel and Godwin.
I thought about using something like \s,\s* but it wouldn't match parts when there is no whitespace around the comma, like between Fred and Frank
Surely, it's a simple fix but I can't figure it out.
I think this will match the commas including the whitespace before and afterwards like you explained in your question.
\s*(?=\,)\,(?<=\,)\s*
This is a positive looahead: (?=\,), it means it matches any whitespace if there is a comma afterwards.
This is a positive lookbehind: (?<=\,), it means it matches any whitespace if there is a comma rigth before.
Try it out yourself. You can use this page to check the output in your browser.

Regex for alphanumeric with at least one digit [duplicate]

This question already has answers here:
RegEx for an invoice format
(5 answers)
Closed 2 years ago.
I'm looking for a regex for Invoice Number in Vbscript
It can have alphanumeric but at least one numeric digit is a must.
I'm using the below regex but it matches ALPHA String INVOICE also. It need to have at least one digit
\b(?=.*\d)[A-Z0-9\-]{5,12}\b
Expected Match String
1233444
M62899M
M828828
783838PTE
A751987
Expected Unmatch String
INVOICE
ubb62727
XYZ
123
If we use ([A-Z0-9]*[0-9]+[A-Z0-9]*), I can't specify the length.
Please suggest a proper regex. Please note its totally different from the suggested duplicate as the requirement, format is different.
The blanket .* in your lookahead will happily skip past the trailing \b if it has to. Make it more constrained, so it can't.
\b(?=[-A-Z]*\d)[A-Z0-9-]{5,12}\b
(I removed the backslash before the -; if you really want to allow a literal backslash, obviously add it back, to the character class in the lookahead also. A dash at beginning or end of a character class is unambiguous and doesn't require a backslash escape; this is also the only way to have a literal dash in a character class in many regex dialects.)

Matching a substring of n numbers, but not if there are any numbers after that [duplicate]

This question already has answers here:
Java RegEx that matches exactly 8 digits
(3 answers)
Closed 5 years ago.
Basically I'm looking for a regex that matches some simple phone numbers.
I want to match numbers in a longer string of text like 123 4567, 891-0111, or 21314151, something that is (hopefully) identified by (\d{3,4}[- ]\d{3,4}|\d{4,8}), but I don't want to match them if they're part of a longer number like 3919503570275.
If I require the next character to be a non-digit or the end of a line, then that next character is also included in the match, which I don't want.
Surround your regex with a lookahead and a lookbehind to reject \d on both sides:
(?<!\d)(\d{3,4}[- ]\d{3,4}|\d{4,8})(?!\d)
Demo.
Note that this would accept a string that looks like a phone number preceded or followed by letters.
Depending on what programming language you use, I suggest to either use negative look-ahead or to use groups to extract the number.
See https://www.regular-expressions.info/lookaround.html for information about lookaround pattern.

Regex for string containing one string, but not another [duplicate]

This question already has answers here:
Regular expression for a string containing one word but not another
(5 answers)
Closed 3 years ago.
Have regex in our project that matches any url that contains the string
"/pdf/":
(.+)/pdf/.+
Need to modify it so that it won't match urls that also contain "help"
Example:
Shouldn't match: "/dealer/help/us/en/pdf/simple.pdf"
Should match: "/dealer/us/en/pdf/simple.pdf"
If lookarounds are supported, this is very easy to achieve:
(?=.*/pdf/)(?!.*help)(.+)
See a demo on regex101.com.
(?:^|\s)((?:[^h ]|h(?!elp))+\/pdf\/\S*)(?:$|\s)
First thing is match either a space or the start of a line
(?:^|\s)
Then we match anything that is not a or h OR any h that does not have elp behind it, one or more times +, until we find a /pdf/, then match non-space characters \S any number of times *.
((?:[^h ]|h(?!elp))+\/pdf\/\S*)
If we want to detect help after the /pdf/, we can duplicate matching from the start.
((?:[^h ]|h(?!elp))+\/pdf\/(?:[^h ]|h(?!elp))+)
Finally, we match a or end line/string ($)
(?:$|\s)
The full match will include leading/trailing spaces, and should be stripped. If you use capture group 1, you don't need to strip the ends.
Example on regex101

Regex IP wildcard Conversion [duplicate]

This question already has an answer here:
Reference - What does this regex mean?
(1 answer)
Closed 4 years ago.
I need to convert the following to Regex. I need the second, and third octets to be a wildcard. I am awful with Regex as I rarely ever need to use it. I have searched the web for a solution. Can someone help with this?
10.(Wildcard).(Wildcard).248
Thanks!
Short and sweet:
10\.\d{1,3}\.\d{1,3}\.248 will get you pretty close, and is relatively simple.
Escape the dot with \. to prevent it from matching any character
Use \d to match any digit character
Use {1,3} to limit the number of consecutive digits to 1, 2, or 3
More complicated, but more exact:
To only match numbers between 0 and 255, you could replace \d{1,3} with ([01]?[0-9]?[0-9]|2[0-4][0-9]|25[0-5]):
10\.([01]?[0-9]?[0-9]|2[0-4][0-9]|25[0-5])\.([01]?[0-9]?[0-9]|2[0-4][0-9]|25[0-5])\.248
- or -
10(\.([01]?[0-9]?[0-9]|2[0-4][0-9]|25[0-5])){2}\.248
Testing/developing regex patterns in the future
There are a lot of regex tester websites out there. I personally use RexexHero.net since I develop .Net applications, but there are other more generic options too such as regexpal.com.
10\.\d+\.\d+\.248
'\.' matches with '.', '\d' matches with any digit. '\d+' matches with 1 or more digits.
Other characters matches with themselves.
You'll need to escape the dot metacharacter with a backslash, but something like this will work:
10\.[0-9]{1,3}\.[0-9]{1,3}\.248
Note: this pattern isn't bulletproof. It doesn't check if the IP address is valid, it only checks if it matches an IP-like pattern. For example, it will match 10.999.999.248.