Solved

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

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

Introduction This Article briefly covers methods of calculating the NPV and IRR variants in Excel as well as the limitations in calculating and interpreting IRR results. Paraphrasing Richard Shockley, author of my favourite finance reference tex…
How to get Spreadsheet Compare 2016 working with the 64 bit version of Office 2016
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…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

696 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