regex


Parsing email with Google Apps Script, regex issue?


I used to be quite proficient in VBA with excel, but I'm currently trying to do something with Google Scripts and I am well and truly stuck.
Basically, I am trying like to extract data out of a standardised email from Gmail into a Google sheet. There are a couple of other threads on the subject which I have consulted so far, and I can get the body of the email into the sheet but cannot parse it.
I am new to regex, but it tests OK on regex101
I am also brand new to Google Script, and even the debugger seems to have stopped working now (it did before, so would be grateful if anyone can suggest why this is).
Here is my basic function:
function processInboxToSheet() {
var label = GmailApp.getUserLabelByName("NEWNOPS");
var threads = label.getThreads();
// Set destination sheet
var sheet = SpreadsheetApp.getActiveSheet();
// Get all emails labelled NEWNOPS
for (var i = 0; i < threads.length; i++) {
var tmp,
message = threads[i].getMessages()[1], // second message in thread
content = message.getPlainBody(); // remove html markup
if (content) {
// search email for 'of:' and capure next line of text as address
// tests OK at regex101.com
property = content.match(/of:[\n]([^\r\n]*)[\r\n]/);
// if no match, display error
var property = (tmp && tmp[1]) ? tmp[1].trim() : 'No property';
sheet.appendRow([property]);
} // End if
// remove label to avoid duplication
threads[i].removeLabel(label)
} // End for loop
}
I can append 'content' to the sheet Ok, but cannot extract the address text required by the regex. Content displays as follows:
NOPS for the purchase of:
123 Any Street, Anytown, AN1 1AN
DATE: 05/05/2017
PRICE: £241,000
Seller’s Details
NAME: Mrs Seller
Thanks for reading :)
The return value of .match() is an array. The first captured group, containing the address, will be at index 1.
Based on the following line after your call to .match(), it looks like the tmp variable should have been assigned that array, not the property variable.
var property = (tmp && tmp[1]) ? tmp[1].trim() : 'No property';
That line says, if .match() returned something that isn't null and has a value at index 1, then trim that value and assign to property, otherwise assign it the string 'No property'.
So, try changing this line:
property = content.match(/of:[\n]([^\r\n]*)[\r\n]/);
To this:
tmp = content.match(/of:[\n]([^\r\n]*)[\r\n]/);

Related Links

How to use array members as the search or replace pattern in bash?
Find foldername of highest version [duplicate]
Extract URL from a list of url in perl
Regular expression to look for a numeric range between two numbers
Regular Expression for Ignoring Files that Begin and End with a Sequence
Matching regular expression only at beginning of line in MATLAB
Regex with Parenthesis
Using Bash Regular Expressions to replace part of matched results
Have trouble identifying and deleting a line - should be simple
Why does this particular exclusionary regex of mine fail?
Editting a string variable in a bash script
smarty regex replace - remove unnecessary chars
Grep in R using OR and NOT
Powershell regex on command output for NLTEST
add quotes to words matching regex in file awk or sed
How to redirect to other host using mod_rewrite in Apache

Categories

HOME
validation
wpf
actions-on-google
web-applications
copy
middleware
genetic-algorithm
xcode8
gluon
spi
go-gorm
socrata
copy-paste
light-inject
development-environment
frequency
signature
lighttpd
git-svn
export-to-excel
html-email
gitlab-ci-runner
formio
jruby
advantage-database-server
software-distribution
pwm
jsgrid
robotium
php-carbon
italic
mathdotnet
yii1.x
webfocus
valueinjecter
expression-trees
file-manager
stl
swipe
background-process
apex-code
mobilefirst-bluemix
dashboard-designer
server-sent-events
bits
json-rpc
transpose
joe-editor
caldroid
internet-explorer-10
procobol
quicksand
distributed-lock
uifont
azure-cdn
domdocument
formstack
stroke
maven-versions-plugin
untagged
mapr
jeditorpane
callkit
ipmi
kie-workbench
nsd
rails-console
mink
post-increment
agents-jade
android-number-picker
blpapi
uiswitch
facebook-comments
fab
jedi
cng
polygons
multiscreen
base-conversion
odoo
javaw
conditional-statements
iscroll4
cvs2git
mklink
culture
razor-2
nhunspell
nine-patch
floating-point-conversion
firebird2.1
jquery-data
gdb-python
google-authorship
xsocket
browserid
qbwc
sticky-footer
ekeventkit
ms-access-97
cellphone
measure
pinging
msn-messenger
run-length-encoding
corporate-policy

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App