ワイルドカードの“%”が 0 個以上の連続した任意の文字列を表し,“_”が任意の 1 文字を表すとき,文字列全体が“%イ%ン_”に一致するものはどれか。
平成23年度 秋期 ITパスポート試験 問73
This question's explanation and your answers so far.
問 73
- ア
正解 The correct answer is ア. First, read the pattern “%イ%ン_” precisely. “%” stands for a string of zero or more consecutive arbitrary characters, so it may match no characters at all or any number of them. “_” stands for any single character, so it must match exactly one character (zero characters is not allowed). And because the question asks about a match of the entire string, the pattern has to cover the string from beginning to end. Putting these together, the conditions for a matching string break down into three parts: (1) there is an “イ” somewhere in the string; (2) there is a “ン” after that “イ”; (3) exactly one character remains after that “ン”, that is, the second-to-last character is “ン”. Option ア, アクセスポイント (access point), has 8 characters: ア, ク, セ, ス, ポ, イ, ン, ト. The 6th character is “イ”, the 7th is “ン”, and the 8th is “ト”. So if we assign “アクセスポ” to the leading “%”, the 6th character “イ” to “イ”, an empty string to the second “%” (zero characters is fine), the 7th character “ン” to “ン”, and the 8th character “ト” to the final “_”, the pattern covers the whole string exactly. Therefore only ア matches. In practice, this kind of pattern matching is the everyday idea behind string searches with the SQL LIKE predicate and behind narrowing down file names. A useful trick is to check the fixed part at the end first: here, just asking whether the second-to-last character is ン immediately rules out イ and ウ, and then you only need to check whether the remaining candidates have an イ before that ン.
Incorrectイ
イベントドリブン (event-driven) has 8 characters: イ, ベ, ン, ト, ド, リ, ブ, ン. The “_” at the end of the pattern must match exactly one character, so exactly one character has to remain after the “ン”. But this string ends with “ン”, and no character at all remains after the 8th character “ン”. If you try to use the other “ン”, the 3rd character, then “トドリブン” — five characters — remains after it, and “_”, which can take only one character, cannot cover them. The decisive point here is that there is no “%” at the end; having an “イ” at the beginning does not make it match. So it does not match.
Incorrectウ
クライアントサーバ (client-server) has 9 characters: ク, ラ, イ, ア, ン, ト, サ, ー, バ. The 3rd character is “イ” and the 5th is “ン”, so it does satisfy the condition that the “イ” comes before the “ン”. However, four characters — “トサーバ” — remain after that “ン”. Since “_” is any single character, not any string, it cannot take those four characters together. Looking at the second-to-last character, it is “ー”, not “ン”, so you can already tell at that point that the condition is not met. This is a choice that is easy to pick by mistake if you confuse the roles of “_” and “%”.
Incorrectエ
リバースエンジニアリング (reverse engineering) has 12 characters: リ, バ, ー, ス, エ, ン, ジ, ニ, ア, リ, ン, グ. Its second-to-last character is indeed the 11th character “ン”, and one character, “グ”, remains after it, so the “ン_” part in the latter half of the pattern is in fact satisfied. However, this string does not contain the character “イ” at all. The 8th character “ニ” is merely a different character with a similar shape, and since “イ” is not a wildcard it must match that very character, so “ニ” cannot stand in for it. Because the “イ” in the first half of the pattern cannot be satisfied, the string does not match as a whole.
- The difference in what each wildcard does — “%” corresponds to a string of zero or more consecutive arbitrary characters, so a length of zero is fine, while “_” is any single character and must consume exactly one character. This difference between “zero characters allowed” and “exactly one character required” decides right from wrong. It is exactly the notation used by the SQL LIKE predicate, and it corresponds to the “*” and “?” used in file name searches.
- How to break down a whole-string match pattern — when the entire string must match, the pattern has to cover it from beginning to end. For “%イ%ン_”, it breaks down into three conditions: there is an イ somewhere, there is a ン after it, and the second-to-last character is ン. Matching the fixed part at the end first lets you narrow down the candidates efficiently.