Need help with regular expression - regex

Is it possible to have this done with one regex?
I need to match only those strings that have exactly one period/dot but the restriction is that that period/dot must not be at the end of the string.
Example:
abc.d will match
.abcd will match
abcd. will not match

Yes, you can do it in one regex:
^[^.]*\.[^.]+$

I really like #codaddict's answer, but how about something without Regex? ( C# code below )
if(a.Split('.').Length>2 || a.EndsWith("."))
{
Console.WriteLine("invalid");
}
What I like is that it is much more clear that you don't want a string with two . and also a . should not be in the end. And this might actually be faster than using a regex.

Related

Match string does not contain substring with regex

Ok, I know that it is a question often asked, but I did not manage to get what I wanted.
I am looking for a regular expression in order to find a pattern that does not contain a particular substring.
I want to find an url that does not contains the b parameter.
http://www.website.com/a=789&c=146 > MATCH
http://www.website.com/a=789&b=412&c=146 > NOT MATCH
Currently, I have the following Regex:
\bhttp:\/\/www\.website\.com\/((?!b=[0-9]+).)*\b
But I am wrong with the \b, the regex match the beginning of th string and stop when it find b=, instead of not matching.
See: http://regex101.com/r/fN3zU5/3
Can someone help me please?
Just use a lookahead to check anything following the URL must be a space or line end.
\bhttp:\/\/www\.website\.com\/(?:(?!b=[0-9]+).)*?\b(?= |$)
DEMO
use this:
^http:\/\/www\.website\.com\/((?!b=[0-9]+)).*$
\b only matches word endings.
^ matches start and end of string
and you dont even need to do it that complicated, If you dont want the url with the b parameter use this:
^http:\/\/www\.website\.com\/(?!b).*$
demo here : http://regex101.com/r/fN3zU5/5
import re
pattern=re.compile(r"(?!.*?b=.*).*")
print pattern.match(x)
This will look ahead if there is a "b=" present.A negative lookahead means it will not match that string.
You had a look at this possibility:
http://regex101.com/r/fN3zU5/6
^http:\/\/www\.website\.com\/[ac\=\d&]*$
only allow &,=,a,c and digits
complete url in group and there should not be a "b=" parameter
if you have more options and you dont want to list them all:
you dont allow a 'b' to be part of your parameters
^http:\/\/www\.website\.com\/[^b]*$
http://regex101.com/r/fN3zU5/7
^http:\/\/www\.website\.com\/(?!.*?b=.*?).*$ works too here "b=" is permitted at any position of the parameter string so you could even have the "b" string as a value of a parameter.
See
http://regex101.com/r/fN3zU5/8
This is what you want. ^http:\/\/www\.website\.com\/(([^b]=[0-9]+).)*$
Its a simple pattern not flexible but it works :
http:\/\/www\.website\.com\/+a=+\w+&+c=+\w+

Regex to match one or more characters

I need to filter a set of strings with a wildcard-type search, like the following:
Looking for He*lo should match "Hello", but not "Helo"
Looking for *ant should match "pant" and "want" but not "ant"
Looking for *yp* should match "gypsy" and "typical"
The * represents one or more characters. I don't mind a handwritten or regex-based search. Any ideas? The typical .NET approach for wildcards matches 0 or more, but I need 1 or more characters. How can I do this?
What you're looking for is the + regex operator
You want the .
For example: he.lo will match your hello, but not helo.
same goes for the rest.
You can easily test it here: http://regexpal.com/.
Do note that .yp. will not match typical nor gypsy, but `.yp.+' will (because of the rest of the characters)

Pattern matching in Perl

I am doing pattern match for some names below:
ABCD123_HH1
ABCD123_HH1_K
Now, my code to grep above names is below:
($name, $kind) = $dirname =~ /ABCD(\d+)\w*_([\w\d]+)/;
Now, problem I am facing is that I get both the patterns that is ABCD123_HH1, ABCD123_HH1_K in $dirname. However, my variable $kind doesn't take this ABCD123_HH1_K. It does take ABCD123_HH1 pattern.
Appreciate your time. Could you please tell me what can be done to get pattern with _k.
You need to add the _K part to the end of your regex and make it optional with ?:
/ABCD(\d+)_([\w\d]+(_K)?)/
I also erased the \w*, which is useless and keeps you from correctly getting the HH1_K.
You should check for zero or more occurrences of _K.
* in Perl's regexp means zero or more times
+ means atleast one or more times.
Hence in your regexp, append (_K)*.
Finally, your regexp should be this:
/ABCD(\d+)\w*_([\w\d]+(_K)*)/
\w includes letters, numbers as well as underscores.
So you can use something as simple as this:
/ABCD\w+/

How can I write a regular expression match for a string that must contain the following characters: thankYou.sjs?donate_page

I need to create a regex query for a Google Analytics goal. The goal url must contain:
thankYou.sjs?donate_page
However, there are many urls than can render this string, all with other modifiers that I don't care about.
Please advise.
#ExplosionPills: I think you forgot about the special meaning of the question mark.
If you don't escape it, your expression:
^thankYou.sjs?donate_path$
Would match
thankYou.sjsdonate_path
or
thankYou.sjdonate_path
Not to mention the special meaning of dot.
So I guess something like this should work:
thankYou\.sjs\?donate_path
Furthermore if it's possible that the donate_path is not the first in the query string you can use this:
thankYou\.sjs\?([^&]*&)*donate_path
Just the string itself will work. If you want only this string, just use the start/end of string zero-width assertions:
^thankYou\.sjs\?donate_path$

regular expression for multiple filenames

I have some files like that
15.58.55.ser 16.22.20.ser 16.36.23.ser 16.40.13.ser 16.59.41.ser 17.05.08.ser 17.14.40.ser 18.14.40.ser 18.20.43.ser
I want to replace these filenames with the following format
image_1.ser image_2.ser ....
I don't know how to achieve it.
please give me some advice.
The regex is quite simple:
(?:\d{2}\.){3}ser
It matches two digits \d{2} and a dot \. three times {3}, ending in ser.
You can see from RegExr that is matches all of your test cases.
However, in order to know how to do the replacement, you'd have to specify a language that you're working with.
Try this(If you need Java code)
String regex = "\\.ser";
fileName = "15.58.55.ser";
System.out.println(filename.replaceAll(fileName.split(regex)[0], "image_1"));
This is just for only one entry. If you want to replace multiple files, do it in For loop or whatever