Solved

Regular Expression Help - finding string of certain length

Posted on 2016-09-07
3
41 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
3 Comments
 
LVL 34

Expert Comment

by:Dan Craciun
Comment Utility
[\w-]{4,}

HTH,
Dan
0
 
LVL 35

Accepted Solution

by:
Terry Woods earned 500 total points
Comment Utility
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
Comment Utility
n/a
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

by Batuhan Cetin Regular expression is a language that we use to edit a string or retrieve sub-strings that meets specific rules from a text. A regular expression can be applied to a set of string variables. There are many RegEx engines for u…
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…
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…

772 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now