Skip to content

Instantly share code, notes, and snippets.

@derekpeterson
Last active August 29, 2015 14:10
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save derekpeterson/5903a52970a7f06cd705 to your computer and use it in GitHub Desktop.
Save derekpeterson/5903a52970a7f06cd705 to your computer and use it in GitHub Desktop.
Comparison of Regular Expressions
import java.util.regex.Matcher;
import java.util.regex.Pattern;
Pattern STATE_REGEX = Pattern.compile( "\\b(?<stateCode>[a-zA-Z]{2})$" );
Matcher matcher = STATE_REGEX.matcher( input );
List<ZipCode> zipCodeList = new ArrayList<>();
if ( matcher.find() ) {
String city = input.substring( 0, matcher.start() - 1 );
city = city.replaceAll( "[^\\w\\s]", " " )
.trim();
String state = matcher.group( "stateCode" );
zipCodeList = getZipCode( city, state );
}
import re
STATE_REGEX = re.compile(r'\b(?P<state_code>[a-zA-Z]{2})$')
match = STATE_REGEX.search(input)
zip_code_list = []
if match is not None:
city = input[:match.start()].replace(r'[^\w\s]', ' ').strip()
state = match.group('state_code')
zip_code_list = get_zip_code(city, state)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment