Solved

Regex Problem

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Auto-indent certain lines in Notepad++ 10 145
Regex in JavaScript to validate US phone number with hyphens 3 56
Create .bat File 16 73
batch file or script 4 50
Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
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 fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
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…

749 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