Solved

Regular Expression Help - finding string of certain length

Posted on 2016-09-07
3
77 Views
Last Modified: 2016-09-21
I need help with a regular expression.  The following expression identifies strings of 4 bytes or more.  However, it does not seem to work when the string has a special character like "-".  

\w{4,}

For Example;

1234                       -->   Found.
23-44                     --> Not found

How can I get this to work with special characters?
0
Comment
Question by:tmajor99
[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
3 Comments
 
LVL 35

Expert Comment

by:Dan Craciun
ID: 41788434
[\w-]{4,}

HTH,
Dan
0
 
LVL 35

Accepted Solution

by:
Terry Woods earned 500 total points
ID: 41789010
The reason for Dan's answer is because the code "\w" matches a single character that is alphabetic, numeric or an underscore. It doesn't match a hyphen character.

The "[\w-]" part of Dan's suggested pattern matches a single character that matches the above description for \w or is a hyphen. You can add more characters between the square brackets if you want other characters to be matched too. Make sure the hyphen is always last in the list or it needs to be prefixed with a backslash character (without the backslash, it indicates a range of characters like 1-5)

[\w^&#-]{4,}

Open in new window


Alternatively, you could just use a different wildcard character such as a period "." which matches any single character (except a newline). Bear in mind it also matches space characters, tab characters, line feed characters etc. There's also the wildcard \S which matches any non-space character.

\S{4,}

Open in new window

0
 

Author Comment

by:tmajor99
ID: 41809158
n/a
0

Featured Post

Technology Partners: 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

As most anyone who uses or has come across them can attest to, regular expressions (regex) are a complicated bit of magic. Packed so succinctly within their cryptic syntax lies a great deal of power. It's not the "take over the world" kind of power,…
Do you hate spam? I do, and I am willing to bet you do as well. I often wonder, though, "if people hate spam so much, why do they still post their email addresses on the web?" I'm not talking about a plain-text posting here. I am referring to the fa…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Suggested Courses

739 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