Regex to compare whether the value of a string is greater than a specific number

  • Thread starter Thread starter Stevie
  • Start date Start date
S

Stevie

Hello

I'm trying to work out the regular expression for carrying out a simple

'greater than' comparison.


I have a directory with files such as asd345.log, asd346.log,
asd347.log and so on.


The regex is to be using in a find command, which should return a list
of files whose numeric portion is greater than, say, 346. In the
example above this would return one file name, asd347.log.


Thanks for any help
Stevie
 
Hello Stevie,

I can think of a couple of other ways (i am sorry but i am not
answering the question directly):

1. Use Compare or CompareOrdinal (depending on your requirement)
functions of the string class.
2. Or call the Trim function of the string class (the one with the
overload in which you can pass a char array. Pass in all the characters
of the character set except the digits) and then parse the result into
an integer for comparison.

Regards,
Vaibhav
 
If you use the following Regular Expression, it should help:

(?<=\w*)\d+(?=.log)

Now, you weren't specific about the rules, but here are my assumptions:

The first part of the file name must be a sequence of 0 or more word
characters.
The second part is a sequence of 1 or more digits.
The third part is the extension ".log"

This regular expression uses a positive look-ahead and a negative
look-behind to assert that the numbers are part of a file name without
capturing the rest of the file name. Once you have the numbers, you convert
them to integers and compare.

--
HTH,

Kevin Spencer
Microsoft MVP
Professional Numbskull

Hard work is a medication for which
there is no placebo.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top