Reputation: 1935
After some solutions on a simple bit of RegEx: I have a piece of Regex which currently does the following:
if(Regex.IsMatch(ffmpegOutputToParse, "x1080|x720"))
{
return true;
}
ffmpegOutputToParse = "Duration: 00:00:28.52, start: 8.000000, bitrate: 7361 kb/s
Stream #0.0: Audio: wmav2, 44100 Hz, stereo, s16, 192 kb/s
Stream #0.1: Video: vc1, yuv420p, 1280x720, 5942 kb/s, 29.97 tb(r)
At least one output file must be specified"
This code works fine. What I would like this code to do now is to not only match "x720" or "x1080" values exactly, but to also return true if the range is between 720-1080.
Many thanks
Upvotes: 2
Views: 323
Reputation: 12458
A regex like (\d+)x(\d+) might let you capture the video resolution. You can convert the strings to integers and then do your comparison. Regexes aren't really the right tool for numeric comparisons, even if it can be made to work.
Upvotes: 0
Reputation: 93636
You don't have to make everything happen with a regex. It is OK to do some things with a regex, and others in non-regex code.
In your case, do it like this (this is in Perl, but you can get the idea):
if ( $ffmpegOutputToParse =~ /x(\d{3,4})/ ) { # Match an x followed by 3 or 4 digits
$rate = $1; # $1 is the first capture group;
$is_ok = ($rate >= 720 && $rate <= 1080); # Is the rate between acceptable values?
....
Regexes are for matching patterns, not comparing numeric values. You can do something like
x(7[2-9][0-9]|[89][0-9][0-9]|10[0-7][0-9]|1080)
But that is far less clear to the reader what that's doing. Also, what if you decide to change the acceptable rate from 720-1080 to 512-1400? You either change one line in my version, or you rewrite the entire regex in @TimPietzcker's version.
Upvotes: 4
Reputation: 1631
I'm nowhere near regex guru, but this should work, although someone might bring something more optimised..
x(72\d)|(7[3-9]\d)|([89]\d\d)|(10[0-7]\d)|(1080)
Upvotes: 0
Reputation: 336098
In that case, you need to spell out the textual representations of all those values:
@"x(7[2-9][0-9]|[89][0-9][0-9]|10[0-7][0-9]|1080)\b"
Explanation:
x # Match x
( # Match either...
7[2-9][0-9] # 720-799
| # or
[89][0-9][0-9] # 800-999
| # or
10[0-7][0-9] # 1000-1079
| # or
1080 # 1080
) # End of alternation
\b # Make sure the number ends here
Upvotes: 3