Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Regular Expression Help - finding string of certain length

Posted on 2016-09-07
3
Medium Priority
?
94 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 2000 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
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…
Suggested Courses

715 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