Having trouble getting a condition to match when creating a Word mail merge. What I want is to match based on the first letter of the field. If the first letter is K-Z, then it should evaluate True.
I have the following in Word:
{ IF { MERGEFIELD Provider } = "[K-Z]*" "Person1" "Person2" }
which does not work. I've tried escaping the square brackets, but this also has had no success.
I can't find anything useful on a search. Has anyone got any ideas how to make this work?
You can't use regex expressions in Word IF fields - not even the limited regex that you can use in Word's Find and Replace function. In an IF field, all you get is the wildcards ? (to match any character) and * (to match multiple characters). Even these have their limitations.
So you have to find another way. One is the tedious one where you enumerate all the possibilities - in this case you could use something like
{ SET KtoZ 0
}{ IF "{ MERGEFIELD Provider }" = "K*" "{ SET KtoZ 1}"
}{ IF "{ MERGEFIELD Provider }" = "L*" "{ SET KtoZ 1}"
}{...
}{ IF "{ MERGEFIELD Provider }" = "Z*" "{ SET KtoZ 1}"
}{IF { REF KtoZ } = 1 "Person1" "Person2" }
(with similar IF fields for M..Y where I have put "..."). If you need to deal with upper/lower case you can add a suitable switch to your MERGEFIELD fields to.
Another way, depending on your situation and on the data source, might be to do the comparison in the data source. That requires either that you can create a view (or in Access, a query) that performs the comparison and returns, for example, a field called KtoZ, or that you can construct your query in SQL and issue it in a Word VBA OpenDataSource call. In the latter case, your data source must use a SQL dialect that lets you do that, and your query must be less than the 255/511 character limit that Word VA imposes.
Related
I have a text like this:
"entity"
{
"id" "5040044"
"classname" "weapon_defibrillator_spawn"
"angles" "0 0 0"
"body" "0"
"disableshadows" "0"
"skin" "0"
"solid" "6"
"spawnflags" "3"
"origin" "449.47 5797.25 2856"
editor
{
"color" "0 0 200"
"visgroupshown" "1"
"visgroupautoshown" "1"
"logicalpos" "[-13268 14500]"
}
}
What would regex expression be to select only that part in Notepad++:
editor
{
"color" "0 0 200"
"visgroupshown" "1"
"visgroupautoshown" "1"
"logicalpos" "[-13268 14500]"
}
First word is always "editor", but the number of lines and content in curly brackets may vary.
editor\s*{\s*(?:\"[a-z]*\"\s*\".*\"\s*)*\}
Demo
Also tested it in Notepad++ it works fine
The simplest way to find everything between curly brackets would be \{[^{}]*\} (example 1).
You can prepend editor\s* on it so it limits the search to only that specific entry: editor\s*\{[^{}]*\} (example 2).
However... if any of the keys or value strings within editor {...} contain a { or }, you're going to have edge cases.
You'll need to find double-quoted values and essentially ignore them. This example shows how you would stop before the first double quote within the group, and this example shows how to match up through the first key-value pair.
You essentially want to repeatedly match those key-value pairs until no more remain.
If your keys or values can contain \" within them, such as "help" "this is \"quoted\" text", you need to look for that \ character as well.
If there are nested groups within this group, you'll need to recursively handle those. Most regex (Notepad++ included) don't handle recursion, though, so to get around this, you copy-paste what you have so far inside of the code if it happens to come across more nested { and }. This does not handle more than one level of nesting, though.
TL;DR
For Notepad++, this is a single line regex you could use.
using $regex in mongodb, I want to find the name B&B Hôtel which contain some special characters like & and ô by typing BB Hotel.
I tried this code:
db.txt.find({ "name": {'$regex': query, $options:'i'}})
where query can be BB Hotel.
You don't want regex search, you want diacritic insensitive text search
"name":{
$text:
{
$search: "\"B&B Hotel\""
$caseSensitive: false,
$diacriticSensitive: false
}
}
Note that $diacriticSensitive defaults to false, but I never trust the defaults. If you are running with older indexes (version 2 or less text index), you may not be able to use the indexes. The escaped " in the search part is to search for this phrase.
I just wrote up a simple google sheets function to fix some URLs. This function works fine in a browser, when passed the array of values manually. When called from google sheets, the function fails for every other row.
This isn't a problem with data, since I can make it work for the "failing" rows by moving the formula down one row, or calling it individually for each cell. I think this may be an issue with regex inside google sheets.
var pattern = /^http:\/\/(.*\/\d\/.*)_(.*)\/(g\d+p.*)$/ig;
function encode(input) {
if (!input) return "";
if (input.map) {
return input.map(encode);
} else {
try {
// same error happens, at this location, w/ or w/o toString()
var matches = pattern.exec(input.toString());
return matches[1] + encodeURIComponent(matches[2]) + matches[3];
} catch (e) {
return "error=" + e.message + " value = [" + input + "] ";
}
}
}
Edit: To make things clearer for those who come after, this also fails the same way when the regex is inside the "else" clause:
else {
var matches = /^(http:\/\/.*\/\d\/.*_)(.*)(\/g\d+p.*)$/ig.exec(input.toString());
... continues as normal
For alternating rows of the data, I get this error message:
error=Cannot read property "1" from null. value = [ http://... ]
I have tried:
Putting the regex inside the try{}
Putting the regex inside the encode{} function
Writing two separate functions (one for doing 1 value)
In the failure case I have data like this:
A1-A8 have URLs in them
B1 has the formula "=encode(A1:A8)"
Data in B1, B3, B5, B7 calculate perfectly
Data in B2, B4, B6, B8 error out (my error message shows up)
Moving the formula to cell "B2" and saying =encode(A2:A8) causes all the "failing" rows to calculate and the others to fail!
The short answer (as confirmed by your comment on the OP) is to remove the final "g" (the global flag) from the regex.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/exec
Syntax regexObj.exec(str)
Parameters str The string against which to match the regular expression.
...
If your regular expression uses the "g" flag, you can use the exec()
method multiple times to find successive matches in the same string.
When you do so, the search starts at the substring of str specified by
the regular expression's lastIndex property (test() will also advance
the lastIndex property).
So it seems you really should only include the global flag when you intend to continue to search for matches in the same string.
As to why it worked in other environments, I'm not sure. Indeed, it seems a bit strange to continue searching from where you left off, even though you are applying exec to an entirely new string. Perhaps the implementation in GAS is a little bit "off" - someone with more knowledge might be able to comment on this.
To elaborate on my comment, the error means that matches is empty or non-existent, which probably means that the regex did not find a match. So it is important to see whether the value of input should match or indeed does not conform to the requirements of the regex.
The regex does the following:
^http:\/\/(.*\/\d\/.*)_(.*)\/(g\d+p.*)$
Debuggex Demo, Matched text:
http://whatever/3/some_thing/g4p/can be anything
^^^^^^^ ^^^ ^ ^^^^
So if any of the following is not found in the URL, no match will be returned:
URL does not start with http:// (but, for instance, https://)
There is no occurrence of: /, a number, /
There is no _
There is no occurrence of /g, some numbers, p
Are you sure the text meets all these requirements every time?
I`m trying to create a mail merge for attendence certificates that brings in an attendence value from an excell spreadasheet.
The attendence column in the spreasheet will hold either a two digit numerical value or blank.
I would like it to display the attendence percentage if there is a value, else display nothing.
The logic in the mailmerge is {If { MERGEFIELD Attendance} = "[0-9]*", "{MERGEFIELD Attendance}", ""}
Could anyone tell me where I am going wrong?
Assuming Attendance holds the percentage (e.g. is 03 for 3%), use { IF "{ MERGEFIELD Attendance}" = "??" "{ ={ MERGEFIELD Attendance } }%" }. You can get rid of some of the quotes and spacing in there if you prefer, but AFAICR you need at least one of the spaces around the "=".
i.e. you can't use regex wildcards (not even the Word version of regex wildcards). The only wildcards allowed here AFAIK are "?" and "*", and you can only use "*" at one end of the expression.
I have a column whose value is
col1
+ASM_DISK_GROUP_TIER1/mydb/data/myfile.111.326
i would like to split the string into something like this
ASM_DISK_GROUP_TIER1 /mydb/data/myfile.111.326 myfile.111.326
(without the +sign)
however
select regexp_substr(col1,'[^/]*') from dual
gives me +ASM_DISK_GROUP_TIER1
and i am clueless how to get the second and the third part i.e
/mydb/data/myfile.111.326 myfile.111.326
Oracle regular expression support is quite limited -- unlike other languages which let you use parentheses to get back parts of the match, in Oracle you can only get the whole matched string (or its start or end position with REGEXP_INSTR).
There are various ways to work around this if you have to, using regexp magic and arithmetic, but in this case you should admit that you are actually just looking for the first and last occurrence of / and code accordingly:
SELECT SUBSTR(col1, 2, INSTR(col1, "/") - 2) "Disk Group",
SUBSTR(col1, INSTR(col1, "/")) "Path",
SUBSTR(col1, INSTR(col1, "/", -1)) "File Name"
FROM ...
(Not tested).