Solved

Regular Expression Help - finding string of certain length

Posted on 2016-09-07
3
69 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

Industry Leaders: 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
URL's dynamic 4 36
remove string in script sql 10 97
do u know a regex code to filter this?equal with lenght 32 6 41
Regex - Capture everything between two strings. 8 26
I have been reconstructing a PHP-based application that has grown into a full blown interface system over the last ten years by a developer that has now gone into business for himself building websites. I am not incredibly fond of writing PHP code o…
Whatever be the reason, if you are working on web development side,  you will need day-today validation codes like email validation, date validation , IP address validation, phone validation on any of the edit page or say at the time of registration…
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…

756 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