user12736831
user12736831

Reputation:

How to Get Specific text from string address using regular expression?

I have been trying to extract the 'XYZ Name' from this address:

*Address: *

XYZ Name
X Address
Y Address
Z Address

if we have an address like this:

Address:
XYZ Name
X Address
Y Address
Z Address

then we can use the following regular expression to get Address and XYZ Name:

var myRegex1 = /Address\s*.*/mi;
var bracket2 = myRegex2.exec(msgBody);

But due to the changed format of Address to *Address: *, this regular expression doesn't work anymore. Any suggestion would be much appreciated.

Upvotes: 2

Views: 510

Answers (1)

user12736831
user12736831

Reputation:

So from the regular expression provided by @Gurmanjot Singh, I was able to extract the required text mentioned in the question. So following snippet can be used for that:

var myRegex2 = / *\*?Address\s*\*?\s*(.+)/g;
var bracket2 = myRegex2.exec(msgBody); 
Logger.log(bracket2[bracket2.length-1]);

Upvotes: 3

Related Questions