Regex for arithmetic expression [duplicate] - java

This question already has answers here:
In a java regex, how can I get a character class e.g. [a-z] to match a - minus sign?
(5 answers)
Closed 5 years ago.
The regex -?\d+ [+|-|*|/] -?\d+ matches expression 1 + 3 without any problems also 1 + -2 without any problems, but I don't know why it does not match 1 - 2. Could you explaing why it does not match the - char correctly?
By my regex I wanted to achieve:
optional - at the beginning
string of digits
whitespace then operator then whitespace
optional - before second stringof digits

A - unescaped in the middle of a character class creates a range. You can escape it or move it to the start or end of the character class. You also don't need/want the |s I'd guess.
You currently make a range between | and | which doesn't really make sense. You also could just use grouping instead of a character class.
(\+|-|\*|/)
With this approach the + and * need to be escaped because they are quantifiers when outside a character class.

Related

Can someone explain regex this regex expression [duplicate]

This question already has an answer here:
Reference - What does this regex mean?
(1 answer)
Closed 2 years ago.
Hi there I'm new to Java and was going through some information on regex and I couldn't comprehend this the following expression:
"^[a-zA-Z\-]+$"
Could someone be kind enough to explain each and every character in this expression?
Thank you.
^ $ # Check if the entire string matches,
[ ]+ # with one or more of the following characters:
a-z # Any lowercase (ASCII) letter
A-Z # Any uppercase (ASCII) letter
\- # Or an "-" (the `\` is used to escape it)
Or in short: this regex checks if a given string consists solely of (ASCII) letters and/or -, and is non-empty.
Try it online.
[a-zA-Z] means all characters a through or A through Z, inclusive.
The "\" inside the square bracket is used as an escape character.
Symbol "+" in the end signified that your regex can occur once or more times.

Having difficulty understanding Java regex interpretation [duplicate]

This question already has an answer here:
Reference - What does this regex mean?
(1 answer)
Closed 3 years ago.
Can someone help me with the following Java regex expression? I've done some research but I'm having a hard time putting everything together.
The regex:
"^-?\\d+$"
My understandning of what each symbol does:
" = matches the beginning of the line
- = indicates a range
? = does not occur or occurs once
\\d = matches the digits
+ = matches one or more of the previous thing.
$ = matches end of the line
Is the regex saying it only want matches that start or end with digits? But where do - and ? come in?
- only indicates a range if it's within a character class (i.e. square brackets []). Otherwise, it's a normal character like any other. With that in mind, this regex matches the following examples:
"-2"
"3"
"-700"
"436"
That is, a positive or negative integer: at least one digit, optionally preceded by a minus sign.
Some regex is composed, as you have now, the correct way to read your regex is :
^ start of word
-? optional minus character
\\d+ one or more digits
$ end of word
This regex match any positive or negative numbers, like 0, -15, 558, -19663, ...
Fore details check this good post Reference - What does this regex mean?
"^-?\\d+$" is not a regex, it's a Java string literal.
Once the compiler has parsed the string literal, the string value is ^-?\d+$, which is a regex matching like this:
^ Matches beginning of input
- Matches a minus sign
? Makes previous match (minus sign) optional
\d Matches a digit (0-9)
+ Makes previous match (digit) match repeatedly (1 or more times)
$ Matches end of input
All-in-all, the regex matches a positive or negative integer number of unlimited length.
Note: A - only denotes a range when inside a [] character class, e.g. [4-7] is the range of characters between '4' and '7', while [3-] and [-3] are not ranges since the start/end value is missing, so they both just match a 3 or - character.

Java regex: select from a set of characters [duplicate]

This question already has an answer here:
Learning Regular Expressions [closed]
(1 answer)
Closed 7 years ago.
I am parsing text and need to detect if any string contains any character NOT including A-Z, 0-9, full stop, comma, plus, minus, or any number of spaces.
I tried the regex expression: "[^A-Z0-9][^.][^,][^-][^+][\S+]"
as well as variations on this, which does not work correctly.
Examples of permissible strings:
1 23842U 96021A 15170.20596865 .00000124 00000-0 00000+0 0 9998
2 23842 0.0589 306.1344 0002868 147.0577 292.5546 1.00269795 70198
Invalid string:
1 2%8!2U 96021A 15170.20596865 .00000124 ^00000-0 00000+0 0 9998
Seems like you want to allow, spaces, alphabets, digits, dot, plus, minus.
Pattern p = Pattern.compile("^[A-Za-z,.+\\s\\d-]+$");

How can I inverse a regex expression to use it in java replaceAll method? [duplicate]

This question already has answers here:
Regular Expression Opposite
(7 answers)
Closed 8 years ago.
I need a last alphabetic character of string example: ABRACADABRA123456. The regex expression [a-zA-Z](?=\d+) give me the match in all my cases. How can I change (inverse) the expression to use it in java method e.g.: "ABRACADABRA123456".replaceAll(<inverse-regex>,"")?
INPUT:ABRACADABRA123456
USE:"ABRACADABRA123456".replaceAll(...)
OUTPUT:A (a last alphabetic character of string)
RESOLVED:System.out.println("ABRACADABRA123456".replaceAll("([\\D]+)([a-zA-Z](?=\\d+))([\\d]+)","$2")));
[a-zA-Z](?=\d+) won't match the last alphabetic character.
System.out.println("ABRACADABRA123456".replaceAll("([A-Za-z])(?=[^A-Za-z]*$)","($1)"));
The above regex would capture the alphabet only if it's followed by any non-alphabetic character zero or more times until the last. So it matches only the last alphabet.
Output:
ABRACADABR(A)123456

Difference between "\\d+" and "\\d++" in java regex [duplicate]

This question already has answers here:
What is the difference between [0-9]+ and [0-9]++?
(2 answers)
Closed 2 years ago.
In java, what's the difference between "\\d+" and "\\d++"?
I know ++ is a possessive quantifier, but what's the difference in matching the numeric string?
What string can match "\\d+" but can't with "\\d++"?
Possessive quantifier seems to be significant with quantifier ".*" only. Is it true?
Possessive quantifiers will not back off, even if some backing off is required for the overall match.
So, for example, the regex \d++0 can never match any input, because \d++ will match all digits, including the 0 needed to match the last symbol of the regex.
\d+ Means:
\d means a digit (Character in the range 0-9), and + means 1 or more times. So, \d+ is 1 or more digits.
\d++ Means from Quantifiers
This is called the possessive quantifiers and they always eat the entire input string, trying once (and only once) for a match. Unlike the greedy quantifiers, possessive quantifiers never back off, even if doing so would allow the overall match to succeed.

Categories