0

(\d+(\.|\,|\\|\/|\-)\d+) I have a regex like this. I'm catching numbers like 15.12, 15/15 with this regex it works correctly. But when I get the inverse with the not operator, it doesn't exactly work properly. Regex with "not" operator. [^(\d+(\.|\,|\\|\/|\-)\d+)]

Let's take "15/12 tons/tonne" as the sample text. Here I expect it to find ton/ton, but it doesn't take the "/" character, it just takes tonton. What am I doing wrong ? How can I fix ?


Example;

15/13 48 km/h  ---> 48 km/h

13.12 ton.     ---> ton.

10,11 ,kwh     ---> ,kwh

9-7 .mm.       ---> .mm.

As seen in the example, I want to find everything except numbers(including whitespaces).

7
  • Your character class is very explicitly not matching / Commented Mar 21, 2022 at 18:53
  • Not sure what you're trying to do here, or why you would expect ton/ton. Are you saying you want to find slashes, dots, commas etc but only with non-numeric characters surrounding them? And how should the space in the middle be interpreted? Commented Mar 21, 2022 at 18:56
  • I'm not exactly sure what you're going after, but post on your bolded example: ^\d+/\d+\s+(tons/tonne)$ Commented Mar 21, 2022 at 19:04
  • (\d+(\.|\,|\\|\/|\-)\d+) with this regex I can find 15/12 in 15/12 tone/ton sentence. I want to find the whitespace ton/tonne by inverting it. I don't want to find things like slash commas separately I want to find anything other than "number character number".@DawoodibnKareem Commented Mar 21, 2022 at 19:08
  • I want to find tone/ton in 15/12 tone/ton sentence. If the sentence was 15-12 abc-abc I would like to find abc-abc. I want to find anything other than "number character number".@Ryan Commented Mar 21, 2022 at 19:11

1 Answer 1

1

[^(\d+(\.|\,|\\|\/|\-)\d+)] is not the inverse of (\d+(\.|\,|\\|\/|\-)\d+)

In regular expressions, anything between the brackets are considered a character set. The ^ is a special character when it's the first character in the set, with it meaning the inverse of the character set.

https://regex101.com/r/7gJANc/4

This is a good way to see exactly which characters are being matched (or not matched) by that pattern.

Last edit:

To find what you're looking for, you can invert each of the instances of \d to \D. \D means "not-digit".

(\D+(\.|\,|\\|\/|\-)\D+) is your pattern but with \D:

https://regex101.com/r/7gJANc/5

It matches tons/tonne

Sign up to request clarification or add additional context in comments.

6 Comments

thanks for your answer. (\D+(\.|\,|\\|\/|\-)\D+) this expression won't work for "15/12 tons of tons" though. What should we do for this situation? I want it to work in both "15/12 tons of tons" and "15/12 tons/tons". actually i want everything except "number charceter number".
Here's a way (?:\d+[.,\\\/-]\d+)?(.*) , but depending on all of your use cases it may not work out. This captures your number/number pattern if it exists. Everything else is captured into group 1.
One note about that pattern, I modified (\.|\,|\\|\/|\-) from your pattern to be [.,\\\/-]. It ends up describing the same thing. The dot doesn't need the backslash within the character set to be interpreted as a literal dot, and the dash doesn't when it's not part of a range.
(?:\d+[.,\\\/-]\d+)?(.*) I guess I'll use that. I'm going to have to modify my code a bit but there doesn't seem to be any other solution. but it may not work depending on all your use cases. What situations could it be?
It depends on what all you're throwing at it. What I remind myself when working with regular expressions is this quote by Jamie Zawinski: "Some people, when confronted with a problem, think 'I know, I'll use regular expressions.' Now they have two problems."
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.