Regex to find State and Zip from Address - regex

Trying to make regex that can get state from address
1- 1234 Bellaire Blvd, Suite 123, Houston, TX 77036
2- 1234 BELLAIRE BL #123, HOUSTON, TX 77036
I have this for state
\w{2}(?=\s\d{1,5})
And this for Zip
(?<=\w{2}\s)\d{5}
FOR STATE
In 1st case above regex is returning "te" from "Suite" and TX for state which is correct
However, in 2nd case it is returning nothing
FOR ZIP
77036 is returned in 1st case and null is returned in 2nd case

I don't think regular expressions are the best way to do this. Rather I'd use an API to parse the address into it's components. You will need state_abbreviation and you're sorted. Example response:
[
{
"input_index": 0,
"candidate_index": 0,
"delivery_line_1": "1 Santa Claus Ln",
"last_line": "North Pole AK 99705-9901",
"delivery_point_barcode": "997059901010",
"components": {
"primary_number": "1",
"street_name": "Santa Claus",
"street_suffix": "Ln",
"city_name": "North Pole",
"state_abbreviation": "AK",
"zipcode": "99705",
"plus4_code": "9901",
"delivery_point": "01",
"delivery_point_check_digit": "0"
},
"metadata": {
"record_type": "S",
"zip_type": "Standard",
"county_fips": "02090",
"county_name": "Fairbanks North Star",
"carrier_route": "C004",
"congressional_district": "AL",
"rdi": "Commercial",
"elot_sequence": "0001",
"elot_sort": "A",
"latitude": 64.75233,
"longitude": -147.35297,
"precision": "Zip8",
"time_zone": "Alaska",
"utc_offset": -9,
"dst": true
},
"analysis": {
"dpv_match_code": "Y",
"dpv_footnotes": "AABB",
"dpv_cmra": "N",
"dpv_vacant": "N",
"active": "Y",
"footnotes": "L#"
}
},
{
"input_index": 1,
"candidate_index": 0,
"addressee": "Apple Inc",
"delivery_line_1": "1 Infinite Loop",
// truncated for brevity
}
]
Hope that helped.

You can match against ', ([A-Z]{2}) ' the state will then be the subpattern matched by the parentheses. In python it would look like this.
import re
s1 = "1- 1234 Bellaire Blvd, Suite 123, Houston, TX 77036"
s2 = "2- 1234 BELLAIRE BL #123, HOUSTON, TX 77036"
m = re.search(', ([A-Z]{2}) ', s1)
print(m.group(1))

Related

Return the names of heroes that begin with I or J, and all their mentors NOSQL

In this exercise that my professor refuses to correct in class, I have to do this query:
Return the names of heroes that begin with I or J, and all their mentors.
Here is the structure of the objects I'm playing with :
{
"_id": {
"$oid": "63495a11d935f6aa5139ee33"
},
"name": "Medea",
"faits": "Quete de la toison d'or avec les argonautes",
"ascendants": [
"Eson",
"Eole"
],
"gender": "female",
"mentor": "Chipute"
}
And here is my attempt at making it work:
db.collection.aggregate(
[
{$project : {{$name :{{ $substr : {$name, 0,1}}: {$or:[ {$eq: 'I'},{$eq: 'J'}]}} },$mentor: 1}}
]
)
And here is te Error returned to me:
Error: clone(t={}){const r=t.loc||{};return e({loc:new Position("line"in r?r.line:this.loc.line,"column"in r?r.column:...<omitted>...)} could not be cloned.
at Object.serialize (node:v8:332:7)
at u (C:\Users\achop\AppData\Local\MongoDBCompass\app-1.33.1\resources\app.asar.unpacked\node_modules\#mongosh\node-runtime-worker-thread\dist\worker-runtime.js:1917:594983)
at postMessage (C:\Users\achop\AppData\Local\MongoDBCompass\app-1.33.1\resources\app.asar.unpacked\node_modules\#mongosh\node-runtime-worker-thread\dist\worker-runtime.js:1917:595591)
at i (C:\Users\achop\AppData\Local\MongoDBCompass\app-1.33.1\resources\app.asar.unpacked\node_modules\#mongosh\node-runtime-worker-thread\dist\worker-runtime.js:1917:600488)
Thank you for your time, there may be numerous mistakes.
If I've understood correctly you can use this query:
It's a simple find query where you can use $regex to find the documents where name starts with (^) I or (|) J.
Also option i is to do the query case insensitive, you can delete that line if you only want values that starts with capital letters.
And the second object in the query is the projection. Which is used to output the values you want. In this case only name and mentor.
db.collection.find({
name: {
$regex: "^(I|J)",
$options: "i"
}
},
{
name: 1,
mentor: 1
})
Example here

Gatling : regex to get a specific value from a JSON

I have this JSON Object coming from an HTML.
{
"isCompany": false,
"accommodations": [
{
"id": "00000000031000006435",
"isChecked": false,
"name": "Théo",
"addressLine1": "Rue des patriotes 40 Y",
"addressLine2": "1050 Ixelles",
"nightsDeclared": 5,
"schoolNightsDeclared": 3,
"schoolNightsAttached": 0,
"taxableNights": 2.0,
"totalPayment": 0.0,
"isInProgress": true,
"isLate": false,
"isPayed": "false",
"deadline": "2021-12-31",
"initialAmount": 0.0,
"remainingAmount": 0.0
},
{
"id": "00000000031000006438",
"isChecked": false,
"name": "Théo",
"addressLine1": "Rue des Gens 45 B",
"addressLine2": "1040 Etterbeek",
"nightsDeclared": 5,
"schoolNightsDeclared": 3,
"schoolNightsAttached": 0,
"taxableNights": 2.0,
"totalPayment": 0.0,
"isInProgress": true,
"isLate": false,
"isPayed": "false",
"deadline": "2021-12-31",
"initialAmount": 0.0,
"remainingAmount": 0.0
}
]
}
I know that in Gatling, it is possible to get the accommodation id by writing this regex :
check(regex(""""accommodations":\[\{"id":"(.*?)"""").saveAs("accommodationId"))
Now my question is, what is the regex that gets the "isInProgress"?
Don't!
Using regex in this specific case could result in your code breaking on slight input changes and will result in unreadable code.
Instead deserialize and access as a dictionary?
[a['id'] for a in json.loads(json_string)['accommodations']]
Also, have you tried to simply replace id with the name of the field you want?
If you insist on using regex for this, check out online regex sites like regex101.com, regexr.com, regextester.com etc. (search for "online regex test") and try to solve this yourself. If your code does not work, ask a question again.
Ok, you have this in your regex
"id":"(.*?)"
You need just change to expected key name as isInProgress or any another. Also pay attention on " around (.*?) - since the value for id a string, they are needed, but value in isInProgress with another type.

Scala Regex: matching a non single character input

For instance the function:
val reg = "[^ ]".r
takes in an input that would match with every character except for the empty space character.
and:
def matchReg(line: String): List[String] = reg.findAllIn(line).toList
Converts the input into a list.
However how would I edit this so that it would match with a non-single character input. Since it seems as though this splits the input such as "14" into "1" and "4" when the values of the regular expression are turned into a list. If the input is "14" I want it to output "14" rather than it being split. Thank you.
EDIT: I have written test cases to explain the type of output I am looking for
"The match" should "take list" in {
assert(matchReg("(d e f)") == List("(", "d", "e", "f", ")"))
}
it should "take numbers" in {
assert(matchReg("(12 45 -9 347 4)") == List("(", "12", "45", "-9", "347", "4", ")"))
}
it should "take operators" in {
assert(matchReg("(- 7 (* 8 9))") == List("(", "-", "7", "(", "*", "8", "9", ")", ")"))
}
With the following case, "take list" and "take operators" passes successfully if I use:
val reg = "[^ ]".r
However "take numbers" does not pass since numbers such as "347" are being split into "3" "4" and "7", when I want them to register as one single number.
This should work for you
val reg = """[^ \(\)]+|\(|\)""".r
You should add some other alternatives if you want to support also [ ] , { } or other operators
# matchReg("(d e f)")
res8: List[String] = List("(", "d", "e", "f", ")")
# matchReg("(12 45 -9 347 4)")
res9: List[String] = List("(", "12", "45", "-9", "347", "4", ")")
# matchReg("(- 7 (* 8 9))")
res10: List[String] = List("(", "-", "7", "(", "*", "8", "9", ")", ")")

How to extract values from JSON response in Postman?

I have a JSON Response :
[{
"dateTime": 1603650600000,
"supplierName": "Mr Orange GP ",
"gender": "male",
"supplierId": "788",
"appointmentId": 454687,
"tentativeAppointmentCode": "5f8ff4a8a9d5f"
}, {
"dateTime": 1603650600000,
"supplierName": "Mr Kennedy test ",
"gender": "male",
"supplierId": "600",
"appointmentId": 573993,
"tentativeAppointmentCode": "5f8ff4a8a9d5f"
}, {
"dateTime": 1603651500000,
"supplierName": "Mr Orange GP ",
"gender": "male",
"supplierId": "788",
"appointmentId": 454688,
"tentativeAppointmentCode": "5f8ff4a8a9d5f"
}, {
"dateTime": 1603651500000,
"supplierName": "Mr Kennedy test ",
"gender": "male",
"supplierId": "600",
"appointmentId": 573994,
"tentativeAppointmentCode": "5f8ff4a8a9d5f"
}]
I need to extract the values of first occurrence of dateTime, AppointmentId and tentativeAppointmentCode.
Tried below mentioned code but didn't work in this case.
var jsonData=JSON.parse(responseBody);
postman.setEnvironmentVariable("dateTime",jsonData.dateTime);
How can I extract these values and store in a variable so that I can use it in other requests?
Assuming, by "reservation ID" you mean tentativeAppointmentCode (reservation ID doesn't occur in your response body), the following will do it:
let jsonData = pm.response.json();
pm.environment.set("dateTime",jsonData[0].dateTime);
pm.environment.set("appointmentId",jsonData[0].appointmentId);
pm.environment.set("tentativeAppointmentCode",jsonData[0].tentativeAppointmentCode);
I have done something like this for storing a token for later use
const jsonData = pm.response.json();
pm.environment.set("token", jsonData.access_token);

autofilling a dict python 2.x

I'm quite new to Python and programming in general, so apologies if this is quite basic or has been asked and answered before. Here is a sample of the data I'm working with:
{
"homeTeam": {
"formation": [
"4",
"4",
"2"
],
"lineupsSorted": [
{
"player": {
"name": "Scott P. Brown",
"slug": "scott-p-brown",
"shortName": "S. P. Brown",
"id": 19889,
"hasImage": true
},
"position": 1,
"shirtNumber": 1,
"substitute": false,
"positionName": "Goalkeeper",
"positionNameshort": "G",
"captain": false
},
{
"player": {
"name": "Carl Winchester",
"slug": "carl-winchester",
"shortName": "C. Winchester",
"id": 110785,
"hasImage": true
},
"position": 2,
"shirtNumber": 27,
"substitute": false,
"positionName": "Midfielder",
"positionNameshort": "M",
"captain": false
},
I am looking to automate populating defined names as I have done manually here:
hometeamPositions =['Goalkeeper','Midfielder','Defender','Defender','Defender','Midfielder','Midfielder','Midfielder','Midfielder','Forward','Forward','Goalkeeper','Defender','Defender','Midfielder','Midfielder','Forward','Forward']
hometeamPlayers = ['S. P. Brown','C. Winchester','M. Onariase','W.
Boyle','J. Cranston','H. Pell','J. Rowe','K. Storer','B. Waters','D.
Wright','D. Holman','R. Lovett','J. Barthram','T. Plavotic','J.
Munns','L. Davis','K. Wootton','J. Dayton']
As I will be repeating this process many hundreds of times with different data (same structure) I was wondering if anyone could give me some tips on automatically building these ranges?
Thanks,
Peter
I'm not sure I understood what is the problem you are trying to solve but I'll try to help.
Assuming you have a dictionary team_dict and you want to create 2 list: hometeamPositions and hometeamPlayers you can use the following code:
hometeamPlayers = []
hometeamPositions = []
for player_dict in teams_dict['homeTeam']['lineupsSorted']:
hometeamPlayers.append(player_dict['player']['shortName'])
hometeamPositions.append(player_dict['positionName'])
The output on your example will be:
hometeamPlayers = ['S. P. Brown', 'C. Winchester']
hometeamPositions = ['Goalkeeper', 'Midfielder']