Solved

Regex Problem

Posted on 2004-09-04
3
234 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
Comment Utility
\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
Comment Utility
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
Comment Utility
^ 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Displaying an arrayList in a listView using the default adapter is rarely the best solution. To get full control of your display data, and to be able to refresh it after editing, requires the use of a custom adapter.
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.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

771 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now