Hi everyone, this is my first question, and I appreciate how great this community is at helping everyone out!
I had a similar problem to this thread - my Regex works like a charm in regex101 , but returns null in google scripts.
I'm trying to return the next line on a number of items:
New booking confirmed!
Jake arrives Oct 22
Send a message to confirm check-in details or welcome Jake.
Jake Flake Phoenix, California, United States Airbnb member since 2015
Send Jake a message: https://www.airbnb.com/z/q/;laskdf;lksjdf
The Getaway https://www.airbnb.com/manage-listing/asdflkjsdf;lkajsfd
Trip details Check-in Sun, Oct 22 Anytime after 4PM
Checkout Thu, Oct 26 11AM
Guests 13
Confirmation code H7XS0SA8 View itinerary https://www.airbnb.com/reservation/itinerary?code=aasdfasdf
Payment $275.50 x 4 nights $1102.00 Cleaning Fees $180.00 Guest Pays $1282.00 Airbnb Fees -$38.46 You earn $1243.54 On the day after your guest checks in, the payment method you supplied will be credited. For details, see your transaction history. Your guest paid $161.15 in Occupancy Taxes. Airbnb remits these taxes on your behalf.
Get ready for Jake's arrival Provide Directions Confirm that your guest knows how to get to your place. Tidy Up Clean up all shared spaces. Make sure your guest has clean linens and towels.
Customer Support
Visit Help Center https://www.airbnb.com/help?eal_exp=asdfasdf
Contact Airbnb https://www.airbnb.com/help/contact_us?eal_exp=asdfasdf
Thanks, The Airbnb Team
Email preferences https://www.airbnb.com/users/notifications?eal_exp=asdfasdf
Airbnb
function getNumberofGuests(message_body) {
var num_guests = message_body.match("Guests[\r\n]+([^\r\n]+)")[1];
Logger.log("num_guests: " + num_guests);
return num_guests;
}
Logs -- num_guests: 13
function getReservationID(message_body) {
var reservationID = message_body.match("Confirmation\scode[\r\n][^\r\n]+")[1];
Logger.log("reservationID: " + reservationID);
return reservationID;
Logs -- reservationID: null
This is driving me crazy, as I've spent 15 hours researching the answer and I still can't figure it out.
The formatting of the body in this display is off for some reason, and I tried for 30 minutes but still couldn't fix it. In the message_body, it should be a new line after "Check-in", "Checkout", "Guests", "Confirmation code", etc. - there is a new line after every element that is currently showing a space.
I also tried the space charater regex, and still got null:
var regExp = new RegExp("Confirmation\scode[\s]^[^\s]+", 'g')
I would love and appreciate any help on this one, thanks Stack Overflow community! -Blaine
I loaded the data into a file and then loaded the file into variable dt in the following code:
function getMyData()
{
var ss=SpreadsheetApp.getActive();
var sh=ss.getActiveSheet()
var dt=myUtilities.loadFile('regex.txt');
var guests = dt.match(/^Guests (\d+)/m)[1];
var cf=dt.match(/^Confirmation code ([0-9A-Z]+)/m)[1];
var ui=HtmlService.createHtmlOutput(dt).append(Utilities.formatString('<br /><strong>Matches:</strong><br />Guests=%s<br />Confirmation Code=%s',guests,cf));
SpreadsheetApp.getUi().showModelessDialog(ui, 'Input Data & Extracted Parameters');
}
After playing with the regex's for a while I got this output which includes a copy of the data:
The m flag is for multiline.
Here's the new code with some of your additional parameters:
function getMyData()
{
var ss=SpreadsheetApp.getActive();
var sh=ss.getActiveSheet()
var dt=myUtilities.loadFile('regex.txt');
var guests = dt.match(/^Guests (\d+)/m)[1];
var cf=dt.match(/^Confirmation code ([0-9A-Z]+)/m)[1];
var chout=dt.match(/^Checkout (.*)$/m)[1];
var chin=dt.match(/^Trip details (.*)$/m)[1];
var payout=dt.match(/Guest Pays ([$.0-9]+)/m)[1];
var ui=HtmlService.createHtmlOutput(dt).append(Utilities.formatString('<br /><strong>Matches:</strong><br />Guests=%s<br />Confirmation Code=%s<br />Checkout=%s<br />Checkin=%s<br />Payout=%s',guests,cf,chout,chin,payout)).setWidth(1200);
SpreadsheetApp.getUi().showModelessDialog(ui, 'Input Data and Extracted Parameters');
}
And here's the output:
I'm guessing that you can figure out how to get the rest of them yourself. I hope this helps.
After taking a look at the code and trying it out, you're getting the null because of what you changed in the match method. The reason why num_guests works is because that it's set up in a way that looks for the word "Guests" and the next line is the result from the array (notice the [1] on the end of the line). That same concept/regex works for the confirmation code portion as well, but you put in the "\\s" and changed the order of some parentheses. Since the search code is in double quotes, you don't need the "\\s". Basically, you have "Confirmation\\scode" when you want "Confirmation code". Although it works in regex101, trying in the Google Apps Script environment will produce actual results.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.