Scott Tavares
Scott Tavares

Reputation: 35

using regex to return integer from string within integer range

I am trying to capture the last numbers from "hpercent=" in the string but only if it falls into a range from 1-25.

sample strings:

desired output from "hpercent": 10, 25, 2

I tried:

just not sure how to put the above regex together in one expression

UPDATE:

Sorry, very new to this. but I guess I need to capture the last number as a group. but as you can see when it captures the last number it IS beyond 25.

I should only capture between 1-25.

Adobe Classifications

Upvotes: 0

Views: 232

Answers (3)

shA.t
shA.t

Reputation: 16958

Just to put your regexes together in one expression:

/([1-9]|1[0-9]|2[0-5])(?=$)/

Upvotes: 0

rgoliveira
rgoliveira

Reputation: 936

Since you didn't specify what's your regex flavor nor programming language you're using, here's a solution using vim:

/\v\=(\d|1\d|2[0-5])$/

What's going on:

  • only capture things comprised between = and EOL ($)
  • then capture:
    • a single digit from 1 to 9, or
    • a 1 followed by any digit (so 10 up to 19), or
    • a 2 followed by anything from 0 to 5 (so anything from 20 up to 25)

It should be easy to convert this to your regex flavor.

Upvotes: 0

user557597
user557597

Reputation:

This should work hpercent=([1-9]|1[0-9]|2[0-5])(?![0-9])

Expanded

 hpercent=
 ( [1-9] | 1 [0-9] | 2 [0-5] )  # (1)
 (?! [0-9] )

Simplified by a number range regex generator tool

Upvotes: 1

Related Questions