Solved

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

Posted on 2016-08-19
2
38 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
need summation of cells of the same row  having same column difference between the two cells 6 32
location range 4 22
Macro 6 48
highlight duplicate entry 16 27
Approximate matching with VLOOKUP and MATCH seems to me to be a greatly under-used technique, and one which is vital for getting good performance out of large lookups. Until recently I would always have advised using an exact match for simplicity an…
This tutorial explains how to create a series of drop-down lists that are dependent upon prior selections to guide (“force”) the user to make the correct selection and reduce data errors within Microsoft Excel. Excel 2010 was used for this tutorial;…
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…
Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…

929 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

16 Experts available now in Live!

Get 1:1 Help Now