I have a Microsoft Word mail-merge where I have to select between two different addresses to print. Input source is an Excel spreadsheet. The spreadsheet has two sets of addresses. The first set is the home address. The second set is the mailing address. The home address is always filled in. The mailing address only contains data if the the person wants mail sent somewhere other than the home address, like a PO Box. I have an if statement set up to choose which set of addresses to print. It works with the exception that when printing the mail address zip code it prints it as a date. For example, Zip code 11575 prints as 9/9/1931. The following is the code.
{ if { MERGEFIELD Mail_Addr1 } = "" "{ADDRESSBLOCK \f "<<_FIRST0_>><< _LAST0_>><< _SUFFIX0_>>
<<_COMPANY_
>><<_STREET1_
>><<_STREET2_
>><<_CITY_>><<, _STATE_>><< _POSTAL_>><<
_COUNTRY_>>" \l 1033 \c 2 \e "United States" \d }" "{ MERGEFIELD FirstName }{ MERGEFIELD LastName }
{ MERGEFIELD Mail_Addr1 }
{ MERGEFIELD Mail_Addr2 \f "
"} {MERGEFIELD Mail_City }, { MERGEFIELD Mail_State }{MERGEFIELD Mail_Zip }"}
I have tried formatting the Mail_Zip column as general, number, text, and even put a ' in front of the zip code to force it to text and the zip code did not print properly.
I tried adding the \# switch with arguments of 00000, '00000', "00000" and all it printed was five zeroes.
How do I get the zip code to print properly?
Well it gets stranger. Trying to eliminate data entry errors I retyped the zip code. Now with no switches it prints 12:00:00 AM. With the \# "00000" switch it prints 00012.
As a test I added { MERGEFIELD Zip } (the home address zip) on its own line and it displays correctly.
Can't figure out why Mail_Zip is different.
Update 1: I closed and re-opened the word doc, as I did a dozen times yesterday, and now it is working.
Word does not care about formatting done in Excel and ignores it.
Try formatting your field in Word.
{MERGEFIELD Mail_Zip \# "00000" }
See Word MVP Graham Mayor's work on formatting switches for fields.
Related
following scenario: I want to display the length of a random word selected from a simple .txt list like this:
composer
circulation
fashionable
prejudice
progress
salesperson
disappoint
I used the following code to display one of these words from the list:
random_word_generator = open("random_words.txt", "r")
random_words = list(random_word_generator)
secret_word = random.choice(random_words)
however, whenever I want to print the length of the word by using:
print("My secret word is " + str(len(secret_word)))
It shows the length of the word - 1 character
like:
progress --> should be 8 letters, but python displays 7...
Do you know how this issue could be solved?
Btw: there are no spaces whatsoever in my .txt file
Kind Regards and many thanks in advance
You should mention the language you're using in the title and the tags.
To your question: Can you please paste the full code you're using? I tried your example, and it displays the correct length of each word (+ 1 for the newline character, which you could remove by calling .strip()), so I'm guessing you do something different.
random_word_generator = open("random_words.txt", "r")
random_words = list(random_word_generator) # ['composer\n', 'circulation\n', 'fashionable\n', 'prejudice\n', 'progress\n', 'salesperson\n', 'disappoint\n']
secret_word = random.choice(random_words) # "progress\n"
print("My secret word is " + str(len(secret_word))) # "My secret word is 9"
print("My secret word w/o newline is " + str(len(secret_word.strip()))) # "My secret word w/o newline is 8"
I am having some issues with regular expression mainly because I think the information I can find is not specifically for powershell and all the samples I have tried either error or don't work as intended. I am trying to replace the first occurrence of a word in a string with another word but not replace any other occurrences of that word. for an example take the string:
My name is Bob, her name is Sara.
I would like to replace the first occurrence of name with baby so the resulting string would be
My baby is Bob, her name is Sara.
I have been working in https://regex101.com/ to try to build and see what is selected as I go but as I said none of these have a powershell flavor of regex. In that I can just turn off the global flag and it seems to select the first occurrence but not in powershell. So I am really at a loss of where to begin all really have at this point is selecting all occurrences of the word namewith:
$test = "My name is Bob, her name is Sara."
$test -replace 'name', 'baby'
One way to replace n times:
$test = "My name is Bob, her name is Sara."
[regex]$pattern = "name"
$pattern.replace($test, "baby", 1)
> My baby is Bob, her name is Sara
You could capture everything before and behind and replace it:
'My name is Bob, her name is Sara.' -replace '(.*?)name(.*)', '$1baby$2'
I am having some issues with regular expression mainly because I think the information I can find is not specifically for powershell and all the samples I have tried either error or don't work as intended. I am trying to replace the first occurrence of a word in a string with another word but not replace any other occurrences of that word. for an example take the string:
My name is Bob, her name is Sara.
I would like to replace the first occurrence of name with baby so the resulting string would be
My baby is Bob, her name is Sara.
I have been working in https://regex101.com/ to try to build and see what is selected as I go but as I said none of these have a powershell flavor of regex. In that I can just turn off the global flag and it seems to select the first occurrence but not in powershell. So I am really at a loss of where to begin all really have at this point is selecting all occurrences of the word namewith:
$test = "My name is Bob, her name is Sara."
$test -replace 'name', 'baby'
One way to replace n times:
$test = "My name is Bob, her name is Sara."
[regex]$pattern = "name"
$pattern.replace($test, "baby", 1)
> My baby is Bob, her name is Sara
You could capture everything before and behind and replace it:
'My name is Bob, her name is Sara.' -replace '(.*?)name(.*)', '$1baby$2'
I am using Net::Whois::Raw to query a list of domains from a text file and then parse through this to output relevant information for each domain.
It was all going well until I hit Nominet results as the information I require is never on the same line as that which I am pattern matching.
For instance:
Name servers:
ns.mistral.co.uk 195.184.229.229
So what I need to do is pattern match for "Name servers:" and then display the next line or lines but I just can't manage it.
I have read through all of the answers on here but they either don't seem to work in my case or confuse me even further as I am a simple bear.
The code I am using is as follows:
while ($record = <DOMAINS>) {
$domaininfo = whois($record);
if ($domaininfo=~ m/Name servers:(.*?)\n/){
print "Nameserver: $1\n";
}
}
I have tried an example of Stackoverflow where
<DOMAINS>;
will take the next line but this didn't work for me and I assume it is because we have already read the contents of this into $domaininfo.
EDIT: Forgot to say thanks!
how rude.
So, the $domaininfo string contains your domain?
What you probably need is the m parameter at the end of your regular expression. This treats your string as a multilined string (which is what it is). Then, you can match on the \n character. This works for me:
my $domaininfo =<<DATA;
Name servers:
ns.mistral.co.uk 195.184.229.229
DATA
$domaininfo =~ m/Name servers:\n(\S+)\s+(\S+)/m;
print "Server name = $1\n";
print "IP Address = $2\n";
Now, I can match the \n at the end of the Name servers: line and capture the name and IP address which is on the next line.
This might have to be munged a bit to get it to work in your situation.
This is half a question and perhaps half an answer (the question's in here as I am not yet allowed to write comments...). Okay, here we go:
Name servers:
ns.mistral.co.uk 195.184.229.229
Is this what an entry in the file you're parsing looks like? What will follow immediately afterwards - more domain names and IP addresses? And will there be blank lines in between?
Anyway, I think your problem may (in part?) be related to your reading the file line by line. Once you get to the IP address line, the info about 'Name servers:' having been present will be gone. Multiline matching will not help if you're looking at your file line by line. Thus I'd recommend switching to paragraph mode:
{
local $/ = ''; # one paragraph instead of one line constitutes a record
while ($record = <DOMAINS>) {
# $record will now contain all consecutive lines that were NOT separated
# by blank lines; once there are >= 1 blank lines $record will have a
# new value
# do stuff, e.g. pattern matching
}
}
But then you said
I have tried an example of Stackoverflow where
<DOMAINS>;
will take the next line but this didn't work for me and I assume it is because we have already read the contents of this into $domaininfo.
so maybe you've already tried what I have just suggested? An alternative would be to just add another variable ($indicator or whatever) which you'll set to 1 once 'Name servers:' has been read, and as long as it's equal to 1 all following lines will be treated as containing the data you need. Whether this is feasible, however, depends on you always knowing what else your data file contains.
I hope something in here has been helpful to you. If there are any questions, please ask :)
Apologizing in advance for yet another email pattern matching query.
Here is what I have so far:
$text = strtolower($intext);
$lines = preg_split("/[\s]*[\n][\s]*/", $text);
$pattern = '/[A-Za-z0-9_-]+#[A-Za-z0-9_-]+\.([A-Za-z0-9_-][A-Za-z0-9_]+)/';
$pattern1= '/^[^#]+#[a-zA-Z0-9._-]+\.[a-zA-Z]+$/';
foreach ($lines as $email) {
preg_match($pattern,$email,$goodies);
$goodies[0]=filter_var($goodies[0], FILTER_SANITIZE_EMAIL);
if(filter_var($goodies[0], FILTER_VALIDATE_EMAIL)){
array_push($good,$goodies[0]);
}
}
$Pattern works fine but .rr.com addresses (and more issues I am sure) are stripped of .com
$pattern1 only grabs emails that are on a line by themselves.
I am pasting in a whole page of miscellaneous text into a textarea that contains some emails from an old data file I am trying to recover.
Everything works great except for the emails with more than one "." either before or after the "#".
I am sure there must be more issues as well.
I have tried several patterns I have found as well as some i tried to write.
Can someone show me the light here before I pull my remaining hair out?
How about this?
/((?:\w+[.]*)*(?:\+[^# \t]*)?#(?:\w+[.])+\w+)/
Explanation: (?:\w+[.])* recognizes 0 or more instances of strings of word characters (alphanumeric + _) optionally separated by strings of periods. Next, (?:\+[^# \t]*)? recognizes a plus sign followed by zero or more non-whitespace, non-at-sign characters. Then we have the # sign, and finally (?:\w+[.])+\w+, which matches a sequence of word character strings separated by periods and ending in a word character string. (ie, [subdomain.]domain.topleveldomain)