Solved

Regex Problem

Posted on 2004-09-04
3
239 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

What Is Transaction Monitoring and who needs it?

Synthetic Transaction Monitoring that you need for the day to day, which ensures your business website keeps running optimally, and that there is no downtime to impact your customer experience.

Question has a verified solution.

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

Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.
Simple Linear Regression
Introduction to Processes

695 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