Match only decimals that don't start with a $ - regex

I have a text file that has many correct prices (decimals that start with a dollar sign). But there are a few that don't have a dollar sign. How can I find those?
In the following string, I only like to match. 72.00
$40.00 72.00 $6
I have tried negative lookbehind, like this (?<!\$)\d+\.\d+ . But this one still matches part of the first decimal.

You can start the match with a word boundary to not trigger the lookbehind on every position when not matching a decimal value:
\b(?<!\$)\d+\.\d+\b
See a regex101 demo.

Related

Regex should find substring that does not start with /

I want to find version numbers in a long string.
Example: "Hellothisisastring 12.3 blabla"
I need to find a substring that is a version number but does not start with "/".
Example: "Hellothisisastring /12.3 blabla"
shouldn't match.
I already build following regex:
[0-9]+.[0-9]
How can I detect a that the version number does not start with "/". The problem is that it is not at the beginning of the string. I already tried with negative lookahead.
(?!/)[0-9]+.[0-9] still matches with a slash before.
Thanks for any help :)
You need to use a lookbehind and include a digit pattern to also fail the positions right after digits:
(?<![\d\/])[0-9]+\.[0-9]+
See the regex demo.
Also, you may match any amount of . + digits using
(?<![\d\/])[0-9]+(?:\.[0-9]+)+
See this regex demo. Details:
(?<![\d\/]) - a negative lookbehind that fails the match if there is a digit or / immediately to the left of the current location
[0-9]+ - one or more digits
(?:\.[0-9]+)+ - one or more sequences of a . and one or more digits.

Regex match an entire number with lookbehind and look ahead logic(without word boundaries)

I am trying to detect if a string has a number based on few conditions. For example I don't want to match the number if it's surrounded by parentheses and I'm using the lookahead and lookbehind to do this but I'm running into issues when the number contains multiple digits. Also, the number can be between text without any space separators.
My regex:
(?https://regex101.com/r/RnTSMJ/1
Sample examples:
{2}: Should NOT Match. //My regex Works
{34: Should NOT Match. //My regex matches 4 in {34
45}: Should NOT Match. //My regex matches 4 in {45
{123}: Should NOT Match. //My regex matches 2 in {123}
I looked at Regex.Match whole words but this approach doesn't work for me. If I use word boundaries, the above cases work as expected but then cases like the below don't where numbers are surrounded with text. I also want to add some additional logic like don't match specific strings like 1st, 2nd, etc or #1, #2, etc
updated regex:
(?<!\[|\{|\(|#)(\b\d+\b)(?!\]|\}|\|st|nd|rd|th)
See here https://regex101.com/r/DhE3K4/4
123abd //should match 123
abc345 //should match 234
ab2123cd // should match 2123
Is this possible with pure regex or do I need something more comprehensive?
You could match 1 or more digits asserting what is on the left and right is not {, } or a digit or any of the other options to the right
(?<![{\d#])\d+(?![\d}]|st|nd|rd|th)
Explanation
(?<![{\d#]) Negative lookbehind, assert what is on the left is not {, # or a digit
\d+ Match 1+ digits
(?! Negative lookahead, assert what is on the right is not
[\d}]|st|nd|rd|th Match a digit, } or any of the alternatives
) Close lookahead
Regex demo
The following regex is giving the expected result.
(?<![#\d{])\d+(?!\w*(?:}|(?:st|th|rd|nd)\b))
Regex Link

How to match the decimal digits which equals 0 at the end of a number in Regex?

I want to remove the zeros at the end of a number coming after the decimal point. To give an example:
12.009000 should match "000"
I have the regex pattern below but it gives an error A quantifier inside a lookbehind makes it non-fixed width and I can't find any solution to fix that. What is the correct pattern to match successfully?
Pattern: (?<=\.[0-9]*)0+$
With Java, you can do it like this.
(\\d) capture digits
followed by 0's
replace with the captured digits.
$1 is the back reference to the capture group
str = str.replaceAll("(\\.\\d+?)0+$","$1");
System.out.println(str);
Note: It will leave 12.000000 as 12.0.
(\d+[.]?\d*?)0*$
One more step is needed to replace the dot for numbers such as 12.000
Click here for demo: Click Here
Or to deal with numbers such as 12.000 in one step:
(?:(\d+)\.0*$)|(?:(\d+[.]?\d*?)0*$)
Click here for demo: Click Here
Here is my attempt:
(?:[.][0-9]*[1-9])(0+)$|([.]0+$)
This assumes that the input string is actually a number (it won't protect against things like xyz.001). It will not match at all if there are no trailing zeros after decimal point; and if there are, it removes:
sequence of 0s preceded by a [1-9] after [.][0-9]*
or
a [.] followed by a sequence of 0s.
The result will always be in the captured group if the regex matches.
([\d.]+?)(0*)
"Find digits and dots, but not greedily, then find trailing zeros"
Group 1 is the number. Group 2 is the trailing zeros.

URL Match Regex: Positive and Negative terms

I'm trying to match both negatives and positives terms in a URL match regex.
"Word1" and "Word2" are negatives ones, and "Word3" is the positive. The URL must have the positive keyword and dont have the negatives to match.
https://example.net
https://example.net/word3 - match
https://example.net/word3/word2 - dont match
https://example.net/word3/word1 - dont match
Currently I'm excluding the homepage too, but with the positive match I think that it will be unnecessary.
^((?!(word1|word2|http(s?):\/\/example\.net\/?($|[?#][&=#+%0-9a-zA-Z]+$))).)*$
How can I use positive and negative matchs in just one string?
If positive lookarounds are supported, you could use:
In your example you use both exemple and example but I have used example here.
^(?!.*\bword[12]\b)(?=.*\bword3\b)https?://example\.net/word\d.*$
That will match:
^ Assert start of the string
(?!.*\bword[12]\b) Negative lookahead to assert what is on the right is not word1 or word2
(?=.*\bword3\b) Positive lookahead to assert what is on the right is word3
https?://example\.net/word\d Match start of the url followed by /word and a digit.
.* Match any character 0+ times
$ Assert end of the string
Regex demo
You can use the following regex:
^(?=.*word3)(?!.*word(?:1|2)).*$
It starts by matching the start of string, then uses a positive look ahead for 'word3', then uses a negative look ahead for 'word' followed by either
1 OR 2.
Finally it matches the rest of the string (if it has passed the test).

Regular expression: Find numbers beginning and ending with different digits

I am looking for a regular expression that can be used to find numbers that begin and end with different digits.
I tried with the following: ^(\d)\d*(?!\1)$
However, this does not work, it gives positive matches for numbers like
121
1233
1441
Where am I getting it wrong? Any ideas?
You could use the positive lookahead (?!\d*\1$) after the capturing group to assert that what follows is not zero or more times a digit ending with group 1:
^(\d)(?!\d*\1$)\d*$
Your regex doesn't actually match the last digit. You should do:
^(\d)\d*(?!\1)\d$
^^
match the last digit!
Your regex just asserts that there isn't the starting digit at the end. Well, an empty string is also "not the starting digit", so it matches things like 1221. You have to tell it to match "a digit that is not the starting digit".
Demo