Solved

Modifying a RegEx pattern to work whether or not there is a space between two elements.

Posted on 2016-08-19
2
33 Views
Last Modified: 2016-08-19
I have a regex pattern that picks up on strings containing a number followed by a letter or letters, but I need it to pick up on strings where there is a space between the number and the letters that come after it. My current pattern is this: s*\d{1,3}[A-M]. It will pick up 18ABC but not 18 ABC. I haven't used RegEx for a while and I can't remember how to write that.  I'm working in Excel if that makes a difference.

Thanks!

John
0
Comment
Question by:gabrielPennyback
2 Comments
 
LVL 20

Accepted Solution

by:
Russ Suter earned 500 total points
ID: 41762844
s*\d{1,3}\s*[A-M]

Open in new window

will give you a match if there is ANY whitespace between the numbers and letters
s*\d{1,3}\s{0,1}[A-M]

Open in new window

will give you a match if there are exactly zero or one whitespace characters between the numbers and letters

If you're specifically looking for the space character (ascii value 32) and not any whitespace (which can include tabs, new lines, or other whitespace depending on regex options) then you could do this:
s*\d{1,3}[ ]{0,1}[A-M]

Open in new window

Or even more explicitly, call out the octal ASCII value of the space character like this:
s*\d{1,3}\40{0,1}[A-M]

Open in new window

I could probably keep coming up with variants that would serve your needs. Regex is extremely flexible. Let me know if you need more than above.

By the way, the Regex you provided would only match "18A" on the string "18ABC". It would not match "18ABC". For that you'd need to include a trailing asterisk which means [zero or more repetitions].
0
 
LVL 1

Author Comment

by:gabrielPennyback
ID: 41762978
That does it, Russ, thanks!
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.

762 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

19 Experts available now in Live!

Get 1:1 Help Now