简体   繁体   English

确定输入中的字符串在Python中是否为单个单词

[英]Determine if string in input is a single word in Python

a brain dead third party program I'm forced to use determines how to treat paths depending if the input supplied is a single word, or a full path: in the former case, the path is interpreted relative to some obscure root directory. 我被迫使用的大脑崩溃的第三方程序决定了如何处理路径,具体取决于所提供的输入是单个单词还是完整路径:在前一种情况下,该路径是相对于某些晦涩的根目录进行解释的。

So, given that the input can be a full or relative path, or a single word (including underscores and dashes, but not spaces), I'm wondering how to write a function that determines if the input is a single "word" as defined above. 因此,假设输入可以是完整或相对路径,也可以是单个单词(包括下划线和破折号,但不能包含空格),我想知道如何编写确定输入是否为单个“单词”的函数,如下所示:以上定义。

For example: 例如:

  • "Public_345" would classify as valid "word" “ Public_345”将被分类为有效的“单词”
  • "/home/path/to/something" would obviously be not “ / home / path / to / something”显然不是
  • "Foo bar" would also not be considered a valid "word" “ Foo bar”也不会被视为有效的“单词”

As string methods are not OK, I am wondering if it would be possible to use regular expression. 由于字符串方法不可行,我想知道是否可以使用正则表达式。 Initially I thought up of something like this: 最初我想到了这样的事情:

match = re.compile(r"[\w-]+")
word = "abdcde_-4"
if len(re.findall(match, word)) == 1:
    print "Single word"

It does feel extremely ugly, however, and I'm pretty sure it doesn't catch corner cases. 但是,它确实感觉非常丑陋,而且我很确定它不会遇到麻烦。 Are there (much) better solutions around there? 周围是否有(很多)更好的解决方案?

You could tweak your regex to match the whole input string so that you don't have to count the matches. 您可以调整正则表达式以匹配整个输入字符串,这样就不必计算匹配项了。 Ie

if re.match(r'\A[\w-]+\Z', word):
  print "Single word"

(compile the regex if you feel so inclined) (如果您倾向于,则编译正则表达式)

\\A and \\Z match the beginning and end of the input string, respectively. \\A\\Z匹配输入字符串的开头和结尾。 So, if your word contains other data in it besides the path, then the above approach does not work. 因此,如果您的word除了路径之外还包含其他数据,则上述方法无效。

>>> r="Foo bar".split()
>>> if len(r) != 1: print "not ok"
...
not ok
>>> if "/" in "/home/path/to/something":
...   print "ok"
...
ok

Just check the string and see if it contains a ' ' (space) or an '\\' (path separator). 只需检查字符串,看看它是否包含' ' (空格)或'\\' (路径分隔符)。 If it does, then it's not a "single word". 如果是这样,那么它不是一个“单词”。

Check the os.path module. 检查os.path模块。 You can use os.path.abspath() to convert a given path into full path (if it is on the same system). 您可以使用os.path.abspath()将给定路径转换为完整路径(如果在同一系统上)。

You can check with the following snippet if the string has any whitespace characters. 您可以使用以下代码段检查字符串是否包含空格字符。

if(str.split()[-1] == str) return True if(str.split()[-1] == str)返回True

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM