nikel
nikel

Reputation: 653

Check if number is greater than 4

I have this text:

HP 400 PROONE İ3-4130T 4GB 500GB 19.5" OB DO    792,40  679,20  582,98  USD     1.462,70    18  100+

The last word is 100+ I want to check if 100 is greater than 4. How can I do this?

Upvotes: 1

Views: 4200

Answers (4)

kayleeFrye_onDeck
kayleeFrye_onDeck

Reputation: 6978

tripleee's answer is fine if you're just trying to find a number larger than 4, but it won't find what you're looking for by itself. For that, you need to explicitly state that it's the last number before the end of the line, and has a plus symbol(+). Use this as your pattern:

\d{2,}?\+$|[5-9]\+$

It does two searches:

Search One:

\d{2,}?\+$

This code searches for at least two digits before a plus (+) symbol and the end of the line($). It can be as small as 10 and as large as you like, as long as it is assumed that it is a whole number, and does not use decimal points.

Search Two:

[5-9]\+$

This code searches for any number between 5 and 9 before a plus symbol (+) and the end of the line ($).

The way regex will typically run this is that it will check for Search one first, then if it gets no hits it will check for Search two. You don't need to individually do these searches if you bundle them together by splitting them with a pipe symbol (|)

Upvotes: 1

This is the regex you'll probably want.

([4-9]\+|\d{2,}\+)

Add whatever flags you need (i.e. gm).

This regex will only match the numbers that are either one character long and are the characters 5 to 9, or numbers that are at least 2 characters long.

For more archaic regex systems, you can use the expanded version:

([5-9]\+|[0-9][0-9]+\+)

which achieves the same thing.

Upvotes: 2

Farvardin
Farvardin

Reputation: 5424

use this:

(([1-4]\d|[5-9])\d*)\+$

DEMO

Upvotes: 1

tripleee
tripleee

Reputation: 189789

As a regex, any number with more than one digit is larger than 4, and any single digit in the range 5-9.

[1-9][0-9]|[5-9]

In some regex dialects, \d is available as a synonym for [0-9] but that sacrifices clarity for very limited gains in this particular context (and you didn't mention any specific tool or dialect in your question, but I have assumed you have access to the | alternation operator).

Regular expressions are an exceptionally poor choice for anything involving arithmetic; you'd do well to consider an alternative approach, if at all possible.

Upvotes: 4

Related Questions