Noor A Shuvo
Noor A Shuvo

Reputation: 2817

Regex pattern in salesforce apex

I am new to regex.

I have a String formatted like below

Street Name 
City, StateCode ZipNumber

for example, the string can be like

50 Connecticut Avenue
Norwalk, CT 06850

or

123 6th Avenue
New York, NY 10013

or

4TH Highway 6
Rule, TX 79547

I am trying to construct a regex here. enter image description here But cannot proceed as I have a little idea about regex. Can you please help me?

Upvotes: 0

Views: 928

Answers (1)

Aaron
Aaron

Reputation: 24812

The following might be enough :

^(?<Street>[^\n]+)\n(?<City>[^,]+), (?<StateCode>[A-Z]{2}) (?<Zip>\d+)$

It captures the following segments in different groups :

  • the first line in a group named Street
  • the part of the second line which precedes the comma in a group named City
  • the next two capital letters in a group named StateCode
  • the following digits in a group named Zip

Upvotes: 1

Related Questions