これはregexを使用して実行可能かどうかを考えましたが、通常の文字列操作/比較を使用する方が簡単だと思いました。これは時間の影響を受けやすいタスクのようには見えないからです。
def find_name(normalized_name, full_name_container):
n = 0
full_name = ''
for i in range(0, len(full_name_container)):
if n == len(normalized_name):
return full_name
# If the characters at the current position in both
# strings match, add the proper case to the final string
# and move onto the next character
if (normalized_name[n]).upper() == (full_name_container[i]).upper():
full_name += full_name_container[i]
n += 1
# If the name is interrupted by a separator, add that to the result
elif full_name_container[i] in ['-', '_', '.', ' ']:
full_name += full_name_container[i]
# If a character is encountered that is definitely not part of the name
# Re-start the search
else:
n = 0
full_name = ''
return full_name
print(find_name('mycompany', 'Some stuff My Company Some Stuff'))
"My Company"が印刷されます。空白やカンマのような、正規化された名前を中断する可能性のある項目のリストをハードコーディングすると、おそらく改善する必要があるでしょう。
恐ろしいです。ありがとう。このメソッドは、実際に私が最初に気にしていた実装ですが、動作させることができませんでした。その間、私は別の実装も見つけました。私はそれを答えとして加えてあなたと他の人がそれをチェックすることができます。 – Lexxxxx