Comparison of Regular Expressions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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