I am looking for a cell formula to find/identify/validate if an adjacent cell contains characters in the correct format.
If there is a character "|", then it must be preceded or followed by any amount of text and then another "|" before the text ends or there is a space, otherwise the cell is wrong.
Examples:
|Name| |Surname| |City| = ok
|Name| |Surname| = ok
|Name| |Surname| New York = ok
New York |Name| |Surname| = ok
|Name| |City |Surname| = wrong
N|ame |City| |Surname| = wrong
|Surname| |ZipCode| = ok
Any ideas?
Best Answer
Another way of stating the problem is:
||
(consecutive|
) are not allowed, nor those enclosing only two of more spaces*.| |
(one space in between) in the text to be validated, it must immediately be preceded by any amount of non-|
text, with a|
or another| |
immediately prior to that, and it must immediately be followed by any amount of non-|
text followed by a|
or another| |
.| |
then there must either be no|
or exactly two|
.Condition 1. is, technically, explicitly ruled out in the question, ("any amount of text" can mean none or space-only is allowed) but it can be inferred from the examples that this is the intent of the OP.
With the conditions re-worded as above a formula-only solution becomes readily apparent as seen applied in the following worksheet:
This is the formula entered into
B2:B11
:Explanation:
The prettified version of the formula is as follows:
The three conditions above can be refactored to the following:
[a] There must be precisely 2 more
|
than those accounted for by the| |
s (the first and the last ones).and
[b] If there exist any
|
, there must be at least two of them, and the first two of them must be separated by at least one non-space character.The formula for [a] is:
The formula for the intra-
|
text validation part of [b] is:The other part of [b] (i.e., that there can't only be one
|
) is taken care of by theCHOOSE()
function, which also takes care of the case when there are no|
(required since this edge case causes errors in formula [b] and an incorrect result for formula [a]).The first argument of the
CHOOSE()
function,maps the possible counts of
|
s to the indexes1
,2
, and3
like so:[0,1,2,3,4,…]
→[1,2,3,3,3,…]
, and thus the function returnsTRUE
for a count of0
,FALSE
for a count of1
, and the result of theAND()
function for all other counts.* The condition not allowing two or more intra-
|
spaces can be relaxed by the use of theTRIM()
function.