Logical operators in regex [duplicate] - regex

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]]

Related

regex only letter and - allowed [duplicate]

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-]+)

Why use a negated empty set in a Regular Expression rather than .? [duplicate]

This question already has answers here:
How to use JavaScript regex over multiple lines?
(8 answers)
Reference - What does this regex mean?
(1 answer)
Closed 2 years ago.
I came across the following RE recently:
(?:<div class="something[^>]+?>[^]+<span class="somethingelse">([\d.])+|<div id="anotherthing[^"]+" class="[^"]*andanother)
The set [^] is valid in JavaScript but not in PCRE. My understanding of it is that it will match "anything not in the set". Since the set is empty, it must be matching anything (one or more times). So why would someone use [^]+ rather than simply .+ ? Is there some very subtle difference, perhaps to do with line endings?
. does not match line endings by default. You can enable the DOTALL option (e.g /.+/s) to make it match line endings as well.

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 must include substring [duplicate]

This question already has answers here:
My regex is matching too much. How do I make it stop? [duplicate]
(5 answers)
How to extract a substring using regex
(14 answers)
Closed 3 years ago.
I'm trying to find the word: <*hasburnt*> in the string below using the this regex: <\*.*(bur).*\*>
But it gives me both <*hasburnt*> <*electrical*>. How do I just get <*hasburnt*> ?
bench testedstarter, starter just makes noise, and <*hasburnt*>
<*electrical*> smell.
Try this: /<.*?(bur).*?>/
Regex101 demo
The reason for ? here is because .* tries to match as much characters as possible, so it also matches <electrical. .*? makes it lazy - trying to match as little as possible, and as such ending the match at <hasburnt>.
EDIT: using ? for the first .* would make <hasburnt> independent of positions of similar strings.

Regular expression to match floats only [duplicate]

This question already has answers here:
Matching numbers with regular expressions — only digits and commas
(10 answers)
regular expression for finding decimal/float numbers?
(9 answers)
Closed 7 years ago.
I need to do a regular expression to match the floats only, what i got is the following :
[\-\+]?[0-9]*(\.[0-9]+)?
But this match also the below
123123132 ,
05/03/1994
I only need want to match the number with the decimal point
Your regex is almost correct for your purpose.
It finds 123123132, because the last part is optional. Removing the ? solves that.
[-+]?[0-9]*(\.[0-9]+)
With that adjustment, it might still find matches in strings like .12/39/3239, if you don't want that to happen, insert enforce matching over the complete string by inserting ^ and $:
^[-+]?[0-9]*(\.[0-9]+)$
How about:
([+-]?[0-9]*\.[0-9]*)
You can see it working here
Here is a regexp handling also existing exponents:
[-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?
Debuggex Demo
Additionally you should force the hole string to be matched to avoid matchings within your date values.
^[-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?$
By the way here is a nice tutorial about matching floating point numbers using regular expressions: http://www.regular-expressions.info/floatingpoint.html.