PC Review


Reply
Thread Tools Rate Thread

Regex. Digits, Letters and Dashes. what am I doing wrong?

 
 
Shapper
Guest
Posts: n/a
 
      30th Aug 2012
Hello,

I need to use a Regex to check if a String is a match where:
1 - Allow Digits, Letters and Dashes (-);
2 - Allow the string to be empty.

I have the following: "^\w+-$"

But this is not working. What am I missing?

Thank You,
Miguel
 
Reply With Quote
 
 
 
 
Brian Cryer
Guest
Posts: n/a
 
      30th Aug 2012
"Shapper" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Hello,
>
> I need to use a Regex to check if a String is a match where:
> 1 - Allow Digits, Letters and Dashes (-);
> 2 - Allow the string to be empty.
>
> I have the following: "^\w+-$"
>
> But this is not working. What am I missing?


Whilst \w will match against any lphanumeric character, the + means you want
one or more. So for zero or more alphanumeric characters you would use:

^\w*$

now for the dash, firstly I'd normally escape a dash although depending on
the context it might not need it, but to play safe: \- Next, you want either
an alphanumeric or a dash, so you'll need a group, so I think what you are
after is:

^(\w|\-)*$

That should be it.

A couple of tips:
1. Whilst there are a number of regex resources on the net, the
"cheat-sheet" which I regularly refer to is:
http://www.cryer.co.uk/glossary/r/re...expression.htm

2. When developing and testing regex expressions I find it useful to use a
standalone regex. So whilst it lacks some refinements I developed the
following site as a single regex tester a few years back:
http://regex.cryer.info/ it uses the ASP.NET regex engine, so should behave
the same way as the regex you are using.

Hope this helps.
--
Brian Cryer
http://www.cryer.co.uk/brian

 
Reply With Quote
 
 
 
 
Shapper
Guest
Posts: n/a
 
      30th Aug 2012
On Thursday, August 30, 2012 11:24:52 AM UTC+1, Shapper wrote:
> Hello,
>
>
>
> I need to use a Regex to check if a String is a match where:
>
> 1 - Allow Digits, Letters and Dashes (-);
>
> 2 - Allow the string to be empty.
>
>
>
> I have the following: "^\w+-$"
>
>
>
> But this is not working. What am I missing?
>
>
>
> Thank You,
>
> Miguel


Hello,

Thank you for the help. It is working fine.

And great Regex tester. I am going to use it in the future.

Thank You,
Miguel
 
Reply With Quote
 
Gene Wirchenko
Guest
Posts: n/a
 
      30th Aug 2012
On Thu, 30 Aug 2012 11:55:07 +0100, "Brian Cryer"
<(E-Mail Removed)> wrote:

[snip]

>Hope this helps.


Well, it helped ME! Thank you for the good links.

Sincerely,

Gene Wirchenko
 
Reply With Quote
 
Arne Vajh°j
Guest
Posts: n/a
 
      30th Aug 2012

On 8/30/2012 6:24 AM, Shapper wrote:
> I need to use a Regex to check if a String is a match where:
> 1 - Allow Digits, Letters and Dashes (-);
> 2 - Allow the string to be empty.
>
> I have the following: "^\w+-$"
>
> But this is not working. What am I missing?


"^[\w-]*$"

would be my suggestion.

(prefix with @ or double backslash in C# source code)

Arne

 
Reply With Quote
 
Anders D. Nygaard
Guest
Posts: n/a
 
      2nd Sep 2012
On 30-08-2012 12:55, Brian Cryer wrote:
> 1. Whilst there are a number of regex resources on the net, the
> "cheat-sheet" which I regularly refer to is:
> http://www.cryer.co.uk/glossary/r/re...expression.htm


Isn't there is a small error in your example for the *? operator?
You write:

For example "b[an]*a" when applied to the word "banana" will
match the entire word, but "b[an]*?a" will match with just "bana".

.... but AFAICS, "b[an]*?a" will match already at "ba".

/Anders.
 
Reply With Quote
 
Brian Cryer
Guest
Posts: n/a
 
      3rd Sep 2012
"Anders D. Nygaard" <(E-Mail Removed)> wrote in message
news:50434b9e$0$291$(E-Mail Removed)...
> On 30-08-2012 12:55, Brian Cryer wrote:
>> 1. Whilst there are a number of regex resources on the net, the
>> "cheat-sheet" which I regularly refer to is:
>> http://www.cryer.co.uk/glossary/r/re...expression.htm

>
> Isn't there is a small error in your example for the *? operator?
> You write:
>
> For example "b[an]*a" when applied to the word "banana" will
> match the entire word, but "b[an]*?a" will match with just "bana".
>
> ... but AFAICS, "b[an]*?a" will match already at "ba".


Yes. You are quite right. Thank you for pointing it out. I'll update it
today.
--
Brian Cryer
http://www.cryer.co.uk/brian

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Count only Letters in cells that contain both letters and numbers TommyB Microsoft Excel Worksheet Functions 1 8th Aug 2009 12:29 AM
Remove dashes between letters and between letters and digits webmaster@msphoto.dk Microsoft Excel Worksheet Functions 7 5th Mar 2008 07:08 PM
How to change from capital letters to small letters and vice versa Tobias Microsoft Word Document Management 1 3rd Dec 2007 01:39 PM
Query is not doing what it's supposed to be doing! =?Utf-8?B?VGF0YWthdQ==?= Microsoft Access Queries 5 5th Jan 2006 12:23 AM
Distinguish Cap Letters and Lowercase letters =?Utf-8?B?TUN5bg==?= Microsoft Access Queries 2 26th May 2005 03:35 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 09:31 AM.