Solved

Regex Problem

Posted on 2004-09-04
3
235 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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Arduino EDI - Programming Language - Voice Recorder 4 81
Java Loop 6 59
how to use laptop or pad camera in vb.net windows application 2 70
Advice in Xamarin 21 78
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.
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

776 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