These forums are now Read Only. If you have an Acrobat question, ask questions and get help from one of our experts.

RegExp within defined fields

carter.henricks
Registered: Jul 7 2008
Posts: 22
Answered

I'm trying to create a Regular Expression which will search specific fields (ex. Text65.1 - Text65.20) in order from bottom to top. What I want it to do is find the first field with one set of characters then a space and then another set of charcters. /[az-AZ]+ [az-AZ]/
Then I want it to convert first name (charcters sets) found into an email address by replacing the "" with a "." and adding "@company.com" after the second set of characters. Would I use the .replace(/ /g,'.') for this? Or would I have the expression create a variable and then write script to edit the variable?
Is this even possible or am I on the wrong track?

My Product Information:
Acrobat Pro 8.1.2, Windows
George_Johnson
Expert
Registered: Jul 6 2008
Posts: 1876
Consider the following sample code:

// Define regular expression for
// the beginning of the string
// followed by one or more word characters
// followed by a space
// followed by one or more word characters
// followed by the end of the string
var re =/^\w+\s\w+$/;

// Here are some possible inputs
var s1 = "Jimmy Buffett";
//var s1 = "James D. Buffett";
//var s1 = "James Delaney Buffett";
//var s1 = "Bubba";

// If we have a match, replace the space
// with a period and add the @domain
if (s1.match(re)) {
s2 = s1.replace(/\s/, ".") + "@margaritaville.edu";
app.alert(s2);
} else {
app.alert("No match, sorry");
}



Test the various strings to see what you get. You may need a more sophisticated match or replace regular expression, but this should get you started.


George
carter.henricks
Registered: Jul 7 2008
Posts: 22
Thanks for your help George! I've been playing around with this but it just seems to keep popping up Jimmy [dot] Buffet [at] Mararitaville [dot] edu regardless of whether or not Jimmy Buffet is written anywhere in the document - however it is generating the correct format and the end result is alright.Is it possible to define the regular express to search within a certain area? And then the }else{ command could be to search the next field?
carter.henricks
Registered: Jul 7 2008
Posts: 22
Thanks for your help George! I've been playing around with this but it just seems to keep popping up Jimmy [dot] Buffet [at] Mararitaville [dot] edu regardless of whether or not Jimmy Buffet is written anywhere in the document - however it is generating the correct format and the end result is alright.Is it possible to define the regular express to search within a certain area? And then the }else{ command could be to search the next field?
George_Johnson
Expert
Registered: Jul 6 2008
Posts: 1876
Yes, that was just sample code with hardcoded input. What you would do is get the input from one or more text fields and test it with code similar to what I posted.

Do you want to replace what the user enters in some fields with the email addresses, or do you want to place the email addresses in separate fields?

George
carter.henricks
Registered: Jul 7 2008
Posts: 22
Sorry for the double post, I was have browser trouble this morning.
I want it to search one column of a page (a revision history) to find the most recent name (the first it comes across if it starts at the bottom) and then assign that created email adress as a variable to be used in an app.mailMsg cTo field.

So it starts at Text65.20 then if there are no characters in the format of "Name Surname" it moves to Text 65.19 and so on, then when it finds a name it replaces it with an email address and uses that variable to send a confirmation email.
George_Johnson
Expert
Registered: Jul 6 2008
Posts: 1876
OK, so what does your code look like currently.

George
carter.henricks
Registered: Jul 7 2008
Posts: 22
var re =/^\[a-zA-Z.-]+\s\[a-zA-Z.-]+$/;

if (s1.match(re)) {
s2 = s1.replace(/\s/, ".") + "@company.com";
app.mailMsg(true, s2, "", "", this.getField("Text22").value, "The document has been signed");
} else {
app.alert("No match, sorry");
}I don't know where to define where the regular express gets its information from and then how to replace the
{
app.alert("No match, sorry");
}
with the next field to be searched.
George_Johnson
Expert
Registered: Jul 6 2008
Posts: 1876
Your loop would look something like the following:

// Loop through fields Text65.1 through Text65.20
for (var i = 1; i < 21; i++) {// Get the current field value
var s1 = getField("Text65." + i).value;

// Define regular expression
var re = /^\[a-zA-Z.-]+\s\[a-zA-Z.-]+$/;

// If we think we can construct a valid email address, send an email
if (s1.match(re)) {
var s2 = s1.replace(/\s/, ".") + "@company.com";
app.mailMsg(true, s2, "", "", this.getField("Text22").value, "The document has been signed");
}
}


George
carter.henricks
Registered: Jul 7 2008
Posts: 22
So I tried the loop code and combinations there of and haven't been able to get something to work.

I have a feeling that something is wrong with the expression itself.

would var re = /^\[a-zA-Z]+\s\[a-zA-Z-]+$/;
recognize "any upper/lower case letter" then a "space" then "any upper/lower case letter or hyphen"?
carter.henricks
Registered: Jul 7 2008
Posts: 22
No it doesn't. I used the original express that you posted and it worked fine, however the loop causes it to open up emails for each match. This code works but is there a way (or is it even necessary) to simplify it.


var s1= getField("Text65.20").value
var s2= getField("Text65.19").value
var s3= getField("Text65.18").value
var s4= getField("Text65.17").value
var s5= getField("Text65.16").value
var s6= getField("Text65.15").value
var s7= getField("Text65.14").value
var s8= getField("Text65.13").value
var s9= getField("Text65.12").value
var s10= getField("Text65.11").value
var s11= getField("Text65.10").value
var s12= getField("Text65.9").value
var s13= getField("Text65.8").value
var s14= getField("Text65.7").value
var s15= getField("Text65.6").value
var s16= getField("Text65.5").value
var s17= getField("Text65.4").value
var s18= getField("Text65.3").value
var s19= getField("Text65.2").value
var s20= getField("Text65.1").value
var s21= getField("Text65.0").value

var re =/^\w+\s\w+$/;


if (s1.match(re)) {
var s0 = s1.replace(/\s/, ".") + "@company.com";
app.mailMsg(true, s0, "", "", this.getField("Text22").value, "The document has been signed");
} else {
if (s2.match(re)) {
var s0 = s2.replace(/\s/, ".") + "@company.com";
app.mailMsg(true, s0, "", "", this.getField("Text22").value, "The document has been signed");
} else {
if (s3.match(re)) {
var s0 = s3.replace(/\s/, ".") + "@company.com";
app.mailMsg(true, s0, "", "", this.getField("Text22").value, "The document has been signed");
} else {
if (s4.match(re)) {
var s0 = s4.replace(/\s/, ".") + "@company.com";
app.mailMsg(true, s0, "", "", this.getField("Text22").value, "The document has been signed");
} else {
if (s5.match(re)) {
var s0 = s5.replace(/\s/, ".") + "@company.com";
app.mailMsg(true, s0, "", "", this.getField("Text22").value, "The document has been signed");
} else {
if (s6.match(re)) {
var s0 = s6.replace(/\s/, ".") + "@company.com";
app.mailMsg(true, s0, "", "", this.getField("Text22").value, "The document has been signed");
} else {
if (s7.match(re)) {
var s0 = s7.replace(/\s/, ".") + "@company.com";
app.mailMsg(true, s0, "", "", this.getField("Text22").value, "The document has been signed");
} else {
if (s8.match(re)) {
var s0 = s8.replace(/\s/, ".") + "@company.com";
app.mailMsg(true, s0, "", "", this.getField("Text22").value, "The document has been signed");
} else {
if (s9.match(re)) {
var s0 = s9.replace(/\s/, ".") + "@company.com";
app.mailMsg(true, s0, "", "", this.getField("Text22").value, "The document has been signed");
} else {
if (s10.match(re)) {
var s0 = s10.replace(/\s/, ".") + "@company.com";
app.mailMsg(true, s0, "", "", this.getField("Text22").value, "The document has been signed");
} else {
if (s11.match(re)) {
var s0 = s11.replace(/\s/, ".") + "@company.com";
app.mailMsg(true, s0, "", "", this.getField("Text22").value, "The document has been signed");
} else {
if (s12.match(re)) {
var s0 = s12.replace(/\s/, ".") + "@company.com";
app.mailMsg(true, s0, "", "", this.getField("Text22").value, "The document has been signed");
} else {
if (s13.match(re)) {
var s0 = s13.replace(/\s/, ".") + "@company.com";
app.mailMsg(true, s0, "", "", this.getField("Text22").value, "The document has been signed");
} else {
if (s14.match(re)) {
var s0 = s14.replace(/\s/, ".") + "@company.com";
app.mailMsg(true, s0, "", "", this.getField("Text22").value, "The document has been signed");
} else {
if (s15.match(re)) {
var s0 = s15.replace(/\s/, ".") + "@company.com";
app.mailMsg(true, s0, "", "", this.getField("Text22").value, "The document has been signed");
} else {
if (s16.match(re)) {
var s0 = s16.replace(/\s/, ".") + "@company.com";
app.mailMsg(true, s0, "", "", this.getField("Text22").value, "The document has been signed");
} else {
if (s17.match(re)) {
var s0 = s17.replace(/\s/, ".") + "@company.com";
app.mailMsg(true, s0, "", "", this.getField("Text22").value, "The document has been signed");
} else {
if (s18.match(re)) {
var s0 = s18.replace(/\s/, ".") + "@company.com";
app.mailMsg(true, s0, "", "", this.getField("Text22").value, "The document has been signed");
} else {
if (s19.match(re)) {
var s0 = s19.replace(/\s/, ".") + "@company.com";
app.mailMsg(true, s0, "", "", this.getField("Text22").value, "The document has been signed");
} else {
if (s20.match(re)) {
var s0 = s20.replace(/\s/, ".") + "@company.com";
app.mailMsg(true, s0, "", "", this.getField("Text22").value, "The document has been signed");
} else {
if (s21.match(re)) {
var s0 = s21.replace(/\s/, ".") + "@company.com";
app.mailMsg(true, s0, "", "", this.getField("Text22").value, "The document has been signed");
} else {
app.alert("Please email a response to whomever last modified the document.");
}
}}}}}}}}}}}}}}}}}}}}