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

Regular expression that allows square-brackets
Reg Expression - split 2 string in between <h3> tags
Perl: Multiple lookbehind regex in a single regex expression
mod_rewrite to nginx rewrite rules
Perl Binary/PCAP regex
Why does grep match all lines for the pattern “\'”
regular expression for C if statementch
Regular expression for search name of methods in java files
Recommendation - Resource/book/pocketbook on regex in SED [closed]
Perl: composing regex thru interpolation when subexpr has capture
RegExp regular expression find & replace whole words only
Given a certain regex pattern, re-arrange and manipulate the contents of a cell
Regular expression“\\|” in strsplit
Parsing sectioned file with augeas
I need search a pattern in a header line of my file and concatenates the next line with Perl
Regular Expression statement to replace text inside two literals

Categories

HOME
lsf
afp
saml
arm
gluon
microsoft-dynamics
predictionio
cross-compiling
editor
jscrollpane
graph-tool
postgresql-9.5
phpbb3
helpers
textmatebundles
nintex-workflow
pwm
py4j
jqplot
google-oauth2
vnc
coqide
instance
directx-12
nest-thermostat
skyscanner
jtapi
aem-6
edit-distance
api-doc
microsoft-translator
sagepay
mindstorms
firedac
consistency
gradle-tooling-api
zfs
maven-jetty-plugin
manifoldjs
sql-mode
jquery-masonry
xssf
gcc4.8
reactivex
maven-versions-plugin
ifc
masspay
dr-memory
cabal-install
sscanf
itamae
configuration-profile
vispy
directsound
xendesktop
sqlfiddle
balsamiq
file-management
pager
pypyodbc
jni4net
sqlj
uac
ssha
appv
iphone-5
bonfire
windows-universal
cordova-cli
wso2as
imake
spiceworks
lwuit-list
cpp-netlib
keyboard-navigation
bson
windward
jquery-mobile-popup
apache-commons-dbcp
xcache
wow64
sparc
hogan.js
square-cube
pixelsense
eject
jcycle
vote
qbwc
hpple
bapi
katta
f2c
duplicate-data
datatemplate
orthographic
nscollectionviewitem
wiki-markup
system-tray
indextank
remote-control
iphone-sdk-3.1

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