Charles Bryant
Charles Bryant

Reputation: 1015

Regular expression A-Z but then exclude string

Really confused, got a bunch of addresses I need to process, I have an address line then a post code

The data is in the format:

ASHLEY CLOSE, HAVEN, BH1

I need to be able to get ASHLEY CLOSE, HAVEN then BH1.

I thought it would be something like:

/^([A-Z ,]+)(?!, BH)/

Upvotes: 0

Views: 81

Answers (2)

scrowler
scrowler

Reputation: 24405

Here's a very basic example:

/(.+,) (BH.+)/gm

# match anything with one or more characters until a comma
# capture any number of these groups until you meet your BH block
# capture BH and the following character

Demo: http://regex101.com/r/xC0jB0

Edit

A better way would be not to look specifically for "BH..." but to just match the group at the end of the string:

/^(.+,) (.+)$/gm

Upvotes: 2

fotanus
fotanus

Reputation: 20116

On ruby:

"ASHLEY CLOSE, HAVEN, BH1" =~ /^([A-| ,]+), ([A-Z0-9]+)/
 => 0 
> puts $1
ASHLEY CLOSE, HAVEN
 => nil 
> puts $2
BH1

I don't think you need an advanced regexp there.

Upvotes: 1

Related Questions