My regex function throws illegal escape exception [duplicate] - regex

This question already has answers here:
Java regex throws java.util.regex.PatternSyntaxException: Illegal/unsupported escape sequence for the letter g
(3 answers)
Closed 2 years ago.
I want to built a calculator which accepts numbers like 1E100 or 1.34E-200. In order to check if the number entered is acceptable i decided to test the condition with my regex:
^-?\\d+\\.?\\d*\\E?\\-?\\d*$
however i get an illegal escape and i dont know why? does reges not accept my E?

You escaped the E, which is not necessary :
^(-?\d+\.?\d*E?\-?\d*)$
Test it here

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

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
^([^?]|..+)$

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.

Regex value of 0.0-5.0 [duplicate]

This question already has answers here:
Using regular expressions to validate a numeric range
(11 answers)
Closed 5 years ago.
I'm trying to create a regex string that allows values 0.0 - 5.0. I need the one decimal point to be required. The string below gets me there, but it also allows 5.1-5.9. How do I prevent 5.1-5.9 from being entered, and allow 5.0?
^[0-5]+(\.[0-9]{1})$
Try this regex:
^([0-4]\.[0-9]|5\.0)$
It matches any number from 0 to 4 then dot then any number.
it also matches 5.0
Note: Your regex has another problem that you used + after [0-5] which also matches 55 for example, so you need to remove the +. You also need to remove {1}, It won't make any change but it's useless.