I have a text box where i get the last name of user. How do I allow only one hyphen (-) in a regular expression?
^([a-z A-Z]*-){1}[a-z A-Z]*$
you can use negative lookahead to reject strings having more than one hyphen:
^(?![^-]+-[^-]+-)[a-zA-Z- ]+$
Matched demo on debuggex.
Another Matched demo on debuggex.
Not Matched Demo demo on debuggex.
Your regular expression allow exactly one -. but I assume that you want to mach "Smith", "Smith-Kennedy", but not "Smith-", to do this you just must move the hyphen to the second group:
^[a-z A-Z]+(-[a-z A-Z]+)?$
BTW, in almost all cases when * is used + is the better solution.
I am assuming you want up to 1 hyphen. If so, the regex you want is
^[a-z A-Z]*-?[a-z A-Z]*$
You can visualize it on www.debuggex.com.
If it matches .*-.*-, then you have more than one hyphen and such string should not be accepted
A problem with your regex is that it forces the user to put a -. You can use ? to make it optional :
^[a-z A-Z]*\-?[a-zA-Z]*$
Related
For example, should not start with h and should contain ap.
Should match apology, rap god, trap but not match happy.
I tried
^[^h](ap)*
but it doesn't match sequences which start with ap like apology.
You may use
^(?!h).*ap
See the following demo. To match the whole string to the end, append .* at the end:
^(?!h).*ap.*
If you plan to only match words following the rules you outlined, you may use
\b(?!h)\w*ap\w*
Or, without a lookahead:
\b([^\Wh]\w*)?ap\w*
See this regex demo and the demo without a lookahead.
#WiktorStribiżew's comment with negative lookahead is correct (you might want to add .* to it if you want to match the whole string).
For completeness, you can also use alternation:
^(?:[^h].*ap|ap).*
Demo: https://regex101.com/r/ecVTGm/1
i want to match following statement in regex:
/
/dev/shm
/var/lib/snap/hello-world/27
any idea?
and this is my last try:
\/((\w+)|(\/\w+-\w+))+
but it seems that, dash-statements treat like a seperate part.
You can use this regex using a negated character class:
^\/(?:[^/]*\/)*.*$
RegEx Demo
RegEx Breakup:
^: Start
\/: Match literal /
(?:[^/]*\/)*: Match 0 or more groups of non-slash strings followed by a /
.*: Match anything after last/`
$: End
If you are a positive person and don't want to use negative classes (as proposed by #anubhava), you can check this fully positive regex:
^(?:(?:\/)|(?:\/\w+)|(?:\/\w+-\w+))+$
RegEx Demo
thanks everyone. all your answers works. but i find a simple approach:
(\/([\w\d\-\/])*)
RegEx Demo
I have got this string [lat:50.000] and I need to get the number out of it, however sometimes it might have a hyphen at the front of it as it could be a minus number.
I have got this regex at the moment [\-]\d+(\.\d{1,10})? however it will only match the number if it has got the hyphen at the front, I need a regex that will match it with and without the hyphen. So I would be left with 50.000 or in some cases -2.000.
Hope this makes sense.
You need a quantifier to state that the hyphen is optional:
[\-]?\d+(\.\d{1,10})?
You can also improve the expression a bit and put the hyphen out of the character class (since it's just one character):
-?\d+(\.\d{1,10})?
Use this regex: \-?\d+\.\d{1,10}
A question mark quantifier ? following a character or group will indicate that it is optional :
-?\d+(\.\d{1,10})?
This is the equivalent of using the {0,1} quantifier.
Yet another one:
(-?\d[\d.,]+)
# - or not (optional)
# followed by at least a digit
# followed by digits, dots and commas
See a demo on regex101.com.
Here is a simple expression
\-?\d*\.?\d*
I'm trying to filter words which is not in the "[ ]".
Why is this not working?
[^\[][\u0000-\u024F]+[^\]]
The reason your expression is not working is that it matches all text inside brackets as well as outside.
This is the best I've been able to do:
/(?:^|])[^[]+/g
It includes the ]s in the match because look-behind is not allowed:
http://regexr.com/3c515
If look-behind were allowed, this would be the ticket:
/(?:^|(?<=]))[^[]+/g
https://regex101.com/r/lK9tS7/3
Because this will match [\u0000-\u024F]+ and 2 character which will be matches by [^\[]. If you want to your regex engine match the whole of pattern you need to use start and end anchors in your regex :
/^[^\[][\u0000-\u024F]+[^\]]$/m
But this will work if your string is contain words in each line, which is not a proper way.
As a better way you can use negative look arounds :
(?<!\[)[\u0000-\u024F]+(?!\])
I currently have the following regex to match a date:
([012]?\d)[\/.-]([0123]?\d)[\/.-]([012]\d{3})\b
This will match, for example 12/24/2011. How would I make the regex so it also matches with a space between the items, 12 24 2011?
Add the space character in the character class:
[\/. -]
^
Your questions has been already answered, but I see some other issues with your regex.
Why do have the word boundary only at the end? You should also use it at the start.
Your regex would also match things like "1.3/2012". You can imporve this easily by using a backreference.
Do you need all those capturing groups?
So my suggestion would be this
\b([012]?\d)([\/. -])([0123]?\d)\2([012]\d{3})\b
^^^^^^^^^ ^^
store the first match
occurence in group 2
group 2
See it here on Regexr
Use this:
([012]?\d)[ \/.-]([0123]?\d)[ \/.-]([012]\d{3})\b
I think that you must add spaces first.
([012]?\d)([\/.-]|\s)([0123]?\d)([\/.-]|\s)([012]\d{3})\b
([012]?\d)[\/.-\s]([0123]?\d)[\/.-\s]([012]\d{3})\b