Solved

Regex Problem

Posted on 2004-09-04
3
236 Views
Last Modified: 2010-04-17
I'm trying to work out a regular expression to deal with monitor resolutions but I'm a bit stuck. The expression should allow any number of digits, then any number of spaces, then an x or an X then any number of spaces and then any number or digits. As it stands I have this...

[0-9\ ]+(x|X)[0-9\ ]+

This is for a Regex in C#. The problem with this is that it still allows letters either before or after the numbers but not either side of the x. The possibilities I'm after are like...

1280x1024
1280 x 1024
1280X1024
1280 X 1024

That kind of thing.

Any ideas?

Darrell
0
Comment
Question by:redneon
  • 2
3 Comments
 
LVL 36

Accepted Solution

by:
Zyloch earned 50 total points
ID: 11981381
\d+\s*[Xx]{1}\s*\d+

If you want it to only be that, with nothing before it or after it, have this:

^\d+\s*[Xx]{1}\s*\d+$
0
 

Author Comment

by:redneon
ID: 11981684
Brillaint. The second one works perfectly, cheers. I don't suppose you could give me a quick run down as to how that works could you? Is it just a case of...

^ - Don't know what this does? I thought it symbolised something that wasn't allowed. i.e. a^ would mean no 'a's.
\d+ - Any (but at least one) number of digits.
\s* - Any number of spaces.
[Xx] - A single X or x.
{1} - Don't know what this does?
\s* - Any number of spaces again.
\d+ Any (but at least one) number of digits again.
$ - Don't know what this means but I'm guessing it symbolises the end of the string?

Darrell
0
 
LVL 36

Expert Comment

by:Zyloch
ID: 11981817
^ symbolizes beginning of string, so what follows has to be at the beginning of the string. (^ can also negate character classes, so if you have [^Xx], it would mean anything BUT X or x.)

The {1} means ONLY 1, I'm not sure if it's necessary, but why take a chance? (especially with how confusing regexes are). This ensures that only one [Xx] can be there. This is commonly used if you want a four letter word: \w{4}, for instance. You can also supply a range, {4,6} 4-6 characters, {4,} at least 4, {,6} most is 6.

And you're right. $ symbolizes end of string.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Computer science students often experience many of the same frustrations when going through their engineering courses. This article presents seven tips I found useful when completing a bachelors and masters degree in computing which I believe may he…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question