regular exprssion for atleast and at most characters - regex

I am new in regular expression. I can't do what want. I have made the regular expression given below
var emailx= /^([a-zA-Z_.]+#[a-z]+[.]+[a-zA-Z]+)$/;
But in the end I want at least 3 character and at most 5 character after the dot sign([.]).
Can you please help me how to do that.
Thank you.

In most Regex flavors (you don't say which you are using) you can use a {min,max} quantifier. For example:
[a-zA-Z]{3,5}
If you happen to be using a flavor without this, then you can do:
[a-zA-Z][a-zA-Z][a-zA-Z][a-zA-Z]?[a-zA-Z]?
Also, if you want exactly one dot, you should use \., not [.]+ which is "one or more dots". And you can make this case-insensitive to simplify:
var emailx = /^([a-z_.]+#[a-z]+\.[a-z]{3,5})$/i;
Finally, note that your regex will discard many, many legal email addresses. Like my !#phrogz.net, for example. Here's a more robust one (that still is not good enough, IMHO):
http://www.regular-expressions.info/email.html

You can use {MIN_OCCURANCE, MAX_OCCURANCE} in your regex.
Like {3,5}

Related

Regex to get match up through second to last occurrence of a string

If I have the following string:
hello/everyone/good/bye/world
I want to match everything up through the second to last forward slash:
hello/everyone/good/
But the number of slashes may vary.
What would the regex be to do this? Been googling around to no avail.
Instead of a regular expression, use split and take advantage of "slicing".
t = "hello/everyone/good/bye/world"
ts = t.split("/")
print "/".join( ts[0:-2] ) + "/"
This prints
hello/everyone/good/
This answer is for python2 (you did not specify which language), there are equivalent commands in some other languages. For example, java has a String.split() method (to complicate things, it requires a regular expression). Consult the documentation for the language you are using.
Maybe try this https://regex101.com/r/bH2vI0/1:
((\w+\/)+(?!\w+\/\w+))
Not sure if sublime allows lookaheads though, but basically just match every word + / except for last two. [A-z] might be better if you don't want digits and _

Capture followed by Digits: Replace Syntax? (Dreamweaver)

When you address a regex capture, things can get tricky when digits follow the capture. In PCRE, I can write
${1}000
to substitute the capture of Group 1 followed by three zeroes.
Does anyone know the equivalent syntax in Dreamweaver replace operations, if any?
If we had a series of "A"s instead of zeroes, we could use:
$1AAAA
But these:
$10000
${1}0000
do not work.
I believe the regex flavor is ECMAScript. Just cannot find the information.
This may not be addressed in the syntax. If so, that would be good to know.
Thank you!
Edit: I should add that this is not matter of life and death as I have a number of grep tools at my fingertips. I would just like to know.
Dreamweaver's regular expression find and replace is supposed to be based on JavaScript's implementation of RegExp. You should be able to just use $1000 in the replacement text. However, like you've found, the replacement groups ($ + group number) are not properly recognized when the replacement text has digits immediately after the grouping token.
FWIW: I've logged a bug on this at http://adobe.ly/DWwish

Regex for just only numbers

I haven't used regular expressions soo much, so I'm having difficulty . I want regex that only validates that the field contains digits, but that does not care about how many.
It should approve 77 and 2377? But do not approve 77.43 or xyz777.
How can I get this using regular expression? Is this expression ^[0-9]+$ ok or not
It's OK. You can just use ^\d+$ for all it matters anyway.
Yes, this regex is perfectly valid and does what you think it does, although if your regex engine supports this you could use \d, whichs stands for [0-9].
A simpler regex would be to invert your match and check for non-digit numbers: \D.

Simple regex for matching up to an optional character?

I'm sure this is a simple question for someone at ease with regular expressions:
I need to match everything up until the character #
I don't want the string following the # character, just the stuff before it, and the character itself should not be matched. This is the most important part, and what I'm mainly asking. As a second question, I would also like to know how to match the rest, after the # character. But not in the same expression, because I will need that in another context.
Here's an example string:
topics/install.xml#id_install
I want only topics/install.xml. And for the second question (separate expression) I want id_install
First expression:
^([^#]*)
Second expression:
#(.*)$
[a-zA-Z0-9]*[\#]
If your string contains any other special characters you need to add them into the first square bracket escaped.
I don't use C#, but i will assume that it uses pcre... if so,
"([^#]*)#.*"
with a call to 'match'. A call to 'search' does not need the trailing ".*"
The parens define the 'keep group'; the [^#] means any character that is not a '#'
You probably tried something like
"(.*)#.*"
and found that it fails when multiple '#' signs are present (keeping the leading '#'s)?
That is because ".*" is greedy, and will match as much as it can.
Your matcher should have a method that looks something like 'group(...)'. Most matchers
return the entire matched sequence as group(0), the first paren-matched group as group(1),
and so forth.
PCRE is so important i strongly encourage you to search for it on google, learn it, and always have it in your programming toolkit.
Use look ahead and look behind:
To get all characters up to, but not including the pound (#): .*?(?=\#)
To get all characters following, but not including the pound (#): (?<=\#).*
If you don't mind using groups, you can do it all in one shot:
(.*?)\#(.*) Your answers will be in group(1) and group(2). Notice the non-greedy construct, *?, which will attempt to match as little as possible instead of as much as possible.
If you want to allow for missing # section, use ([^\#]*)(?:\#(.*))?. It uses a non-collecting group to test the second half, and if it finds it, returns everything after the pound.
Honestly though, for you situation, it is probably easier to use the Split method provided in String.
More on lookahead and lookbehind
first:
/[^\#]*(?=\#)/ edit: is faster than /.*?(?=\#)/
second:
/(?<=\#).*/
For something like this in C# I would usually skip the regular expressions stuff altogether and do something like:
string[] split = exampleString.Split('#');
string firstString = split[0];
string secondString = split[1];

Match last word after /

so, i have some kind of intern urls: for example "/img/pic/Image1.jpg" or "/pic/Image1.jpg" or just "Image1.jpg", and i need to match this "Image1.jpg" in other words i want to match last character sequence after / or if there are no / than just character sequence. Thank you in advance!
.*/(.*) won't work if there are no /s.
([^/]*)$ should work whether there are or aren't.
Actually you don't need regexp for this.
s="this/is/a/test"
s.substr(s.lastIndexOf("/")+1)
=> test
and it also works fine for strings without any / because then lastIndexOf returns -1.
s="hest"
s.substr(s.lastIndexOf("/")+1)
=> hest
.*/([^/]*)
The capturing group matches the last sequence after /.
The following expression would do the trick:
/([\w\d._-]*)$
Or even easier (but i think this has also been posted below before me)
([^/]+)$
A simple regex that I have tested:
\w+(.)\w+$
Here is a good site you can test it on: http://rubular.com/
In Ruby You would write
([^\/]*)$
Regexps in Ruby are quite universal and You can test them live here: http://rubular.com/
By the way: maybe there is other solution that not involves regexps? E.g File.basenam(path) (Ruby again)
Edit: profjim has posted it earlier.
I noticed you said in your comments you're using javascript. You don't actually need a regex for this and I always think it's nice to have an alternative to using regex.
var str = "/pic/Image1.jpg";
str.split("/").pop();
// example:
alert("/pic/Image1.jpg".split("/").pop()); // alerts "Image1.jpg"
alert("Image2.jpg".split("/").pop()); // alerts "Image2.jpg"
Something like .*/(.*)$ (details depend on whether we're talking about Perl, or some other dialect of regular expressions)
First .* matches everything (including slashes). Then there's one slash, then there's .* that matches everything from that slash to the end (that is $).
The * operates greedily from left to right, which means that when you have multiple slashes, the first .* will match all but the last one.