Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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