tags:

views:

47

answers:

2

Hello,

I know need some help with regex I am not very smart when it comes to figuring this out but I will get there.

My question:

I need to remove banner sizes from a string,

banner sizes can be 486X60 or 49 X 120

How can I use regex to replace this with blank?

+2  A: 
s/\d+\s*[Xx]\s*\d+//
chaos
don't you mean "\s\d+\s*" instead of "s/\d+\s*"?
Keng
Not really, no. The information OP provided isn't sufficient to know whether that's appropriate.
chaos
A: 

The basic idea is something like

1 or more digits
a space (optional)
X
another space (optional)
1 or more digits

The exact syntax depends on what language you're using, but it would look something like

s/\d+[ ]?X[ ]?\d+//

or, if you want to be sure that the X either has spaces on both sides or none at all, you can separate the two cases:

s/(\d+ X \d+)|(\d+X\d+)//

This will cover exactly the two example cases you mentioned. If you want to allow any amount of whitespace around the X, or allow the X to be lower case, chaos's solution covers that.

Michael Myers
This is what I ended up with thanks, let me know if you guys spot any thing wrong with it. [0-9]+\s*[Xx]\s[0-9]+Also, here is a tool that I use at this site. http://regex.larsolavtorvik.com/Thanks guys
Ok, that's equivalent to chaos's answer except that you're now *requiring* a whitespace character after the X. If you want to make it optional, add a * after the second \s.
Michael Myers