Solved

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

Posted on 2016-08-19
2
50 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: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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

A little background as to how I came to I design this code: Around 5 years ago I designed an add-in that formatted Excel files to a corporate standard, applying different cell colours and font type depending on whether the cells contained inputs,…
Improved? Move/Copy Add-in Replacement - How to avoid the annoying, “A formula or sheet you want to move or copy contains the name XXX, which already exists on the destination worksheet.” David Miller (dlmille)  It was one of those days… I wa…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.

809 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