regex only letter and - allowed [duplicate] - regex

This question already has answers here:
Regular expression for alpahbet,underscore,hyphen,apostrophe only
(5 answers)
Closed 2 years ago.
I want to make a pattern for input.
I have this [A-Za-z]|-, but if I type dsadsa$ this, the special characters still allowed and I need only
big and small letters, and - .

You can escape - character:
[A-Za-z\-]

Your expression should be like following:
([A-Za-z\-]+)
([\w-]+)

Related

Logical operators in regex [duplicate]

This question already has answers here:
Exclude characters from a character class
(5 answers)
Character class subtraction, converting from Java syntax to RegexBuddy
(3 answers)
Subtraction in regex doesn't get results
(4 answers)
Javascript regex character exclusion
(4 answers)
Closed 28 days ago.
I would like to know if there's any logical and operators in a regex, for example I want to do something like match a-z and A-Z but not e/E/i/I.
I tried something like
[a-zA-Z]&[^eEiI]
But it's just plain wrong, there's no such operator in any regexes.
But weirdly there's an or operator that can be used within groups like (x|y).
So I was wondering if there's any work around when there's a need to include a logical and condition in regexes.
You have 2 options:
1: Use negative lookahead:
(?![eEiI])[a-zA-Z]
2: Use negated character class and exclude few characters:
[a-df-hj-zA-DF-HJ-Z]
Additionally, if you are using Java as regex flavor then you can use:
[a-zA-Z&&[^eEiI]]

Regex how prevent string containing only the "?" character [duplicate]

This question already has an answer here:
Learning Regular Expressions [closed]
(1 answer)
Closed 2 years ago.
I'm searching for a Regex to avoid letting user to enter a string with only a single ? character.
Here are examples of what I want to achieve:
? Not Ok (I want to avoid letting this)
?a Ok
a? Ok
?? Ok
??? Ok
This matches either any one- character string which is different from ? or any string with at least two characters
^([^?]|..+)$

Need a regular expression which accepts 4 numbers and 1 character exactly in the same format [duplicate]

This question already has answers here:
Reference - What does this regex mean?
(1 answer)
Learning Regular Expressions [closed]
(1 answer)
Closed 3 years ago.
I need a regular expression which accepts following format
1111A or 1234B.
I used /([0-9]{0,4})&([A-Z])/ but not able to get it.
Try using ^\d{4}[A-Z]$
const re = RegExp(/^\d{4}[A-Z]$/);
console.log(re.test('1111A'));
console.log(re.test('1A111'));
console.log(re.test('1111AA'));

How to match all strings that has only one dot using regular expression [duplicate]

This question already has an answer here:
Reference - What does this regex mean?
(1 answer)
Closed 3 years ago.
I need to capture strings containing only one dot. String will mostly contains domain names like
test.com, fun.test.com, lesh.test.com.
I need to check only the first one and to ignore the string that has more than one dots.
How can I do this using regex?
Like this :
^[^.]+\.[^.]+$
Check explanations https://regex101.com/r/mn7Ccr/1

Regex to Match Multiple Words In Different Order [duplicate]

This question already has answers here:
What is a word boundary in regex?
(13 answers)
Closed 4 years ago.
string sample: "universal studios japan"
How do i make so that it matches with "japan universal studios"
AND also with "japan univer"
Right now I'm using the following to regex :
^(?=.*\bjapan\b)(?=.*\buniversal\b)(?=.*\bstudios\b)
which works but
^(?=.*\bjapan\b)(?=.*\buniver\b)
does not work. It has to be a complete match for the second word..
^(?=.*\bjapan\b)(?=.*\buniversal\b) would work..
What changes do i need to make?
^(?=.*\bjapan\b)(?=.*\buniver(?:sal)?\b)
You can make sal optional.
See demo.
https://regex101.com/r/wDUC7j/1