Regex for just only numbers - regex

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.

Related

regular expression - excluding specific chars

I am trying to write a regular expression matching a set without some chars.
For example, it matches [ a-zA-Z]* but excludes i,o,q,I,O,Q.
So: "A fat cat" matches, "Boy" doesn't.
Looks like it can be [ a-hj-npr-zA-HJ-NPR-Z]*.
Is there a simpler version for this?
Btw, I'm using it in PostgreSQL, but I think it should be a standard expression.
You can use negative lookahead for this as Postgresql support lookaheads:
(?![ioqIOQ])[A-Za-z ]
To make it match complete line use:
^(?:(?![ioqIOQ])[A-Za-z ])+$
RegEx Demo
Based on #Anubhava's answer, but extending to an entire string rather than just one character,
^(?=[^ioqIOQ]*$)[ A-Za-z]*$
The (?=...) is a positive lookahead -- the opposite of the negative lookahead in Anubhava's answer. We are requiring all matches to also match the constraint [^ioqIOQ].
You could also implement the repetition over the entire string with
^((?![ioqIOQ])[ A-Za-z])*$
but it seems a lot less efficient. (I have not performed any timings, though.)
Don't need fancy lookaheads/behinds just use more, but smaller, character ranges.
You'll want something like ^[a-hj-npr-zA-HJ-NPR-Z ]*$.
Added a space to match sentences
You can see test this on-line here at debuggex

regular exprssion for atleast and at most characters

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}

Extract number + letter combination from string

I'm intrested in extracting c8127c6ea6a44c109b5e35ce61cd4b0096a9c6dc from a string that looks like this:
?t=c8127c6ea6a44c109b5e35ce61cd4b0096a9c6dc'
Here is my attempt at capturing the result in to a group.
?t=([a-e]\d+)'
Could anyone point me in the right direction, since this obviously isn't working?
http://regexr.com?383s6
You wanna put the \d in the [a-e] block and escape the ?:
\?t=([a-f\d]+)'
(and I assume you're looking for hexadecimal so it should be a-f?)
You can use this regex:
\?t=([a-e0-9]+)'
OR usig negation:
\?t=([^']+)'
In Ruby language you can try Rubular tool online to test your regular expressions.
\?t=(\w+)'
should do the match.

How to set up REGEX that doesn't match anything?

Just for curiosity.
Is it possible to create a regular expression that will not match any string, including an empty string?
Yes.
Here are a few examples.
.^
$.
(?!)
Naturally, there are an infinite number of such expressions.
This regex should never match anything (provided you do not use single-line or multi-line modifiers):
$x^
How about /^$x/. When I try it with ruby, it seems to work.

is it the right reqular expression

i have following regular expression but it's not working properly it takes only three values after # sign but i want it to be any number length
"/^[a-zA-Z0-9_\.\-]+\#([a-zA-Z0-9\-]+\.)+[a-zA-Z0-9]{2,4}$/"
this#thi This is validated
this#this It is not validating this expression
Can you please tell me what's the problem with the expression...
Thanks
If you want your regex to match "any number length" then why are you using {2,4}?
I think a better example of the strings you're trying to match might give others a better idea of what you want, because based on your regex it is a bit confusing what you're looking for.
Try this:
^[a-zA-Z0-9_.-]+#([a-zA-Z0-9-]+\.)+[a-zA-Z0-9]{2,4}$
The main problem is that you didn't escape the dot: \.. In regular expression the dot matches everything (mostly), making your regex quite liberal.