简体   繁体   English

使用带有输入功能的python中带引号和不带引号的模式搜索的差异

[英]Diffrence using pattern search with and without Quotes in python with input fuction

What and why it makes diffrence while using Patt_Search as re.search(Patt_Search, line) then it works and re.search('Patt_Search', line) don't work , as i ran it and did not seen any error though.. it simply gets out.. 什么以及为什么在使用Patt_Search作为re.search(Patt_Search, line)然后起作用,而re.search('Patt_Search', line)却不起作用,因为我运行了它,但是没有看到任何错误。它只是下车..

Saying that, it works without the quote and don't work with quotes '' . 如此说来,它在没有引号的情况下有效,并且在没有引号''不起作用''

The way it works as Below: 它的工作方式如下:

$ cat Search_string_input.py
#!/usr/bin/python3
import re
File = input("Please Enter the File Name : ")
Patt_Search = input("Please Enter the Pattern you want to search : ")
ldap = open(File, mode='rt', encoding='utf-8')
for line in ldap:
    line = line.rstrip()
    #if re.search('12:00:16', line) :
    if re.search(Patt_Search, line) :
        print(line)

It works as below: 它的工作原理如下:

$ ./Search_string_input.py
Please Enter the File Name : ldap_data
Please Enter the Pattren you want to serach : 12:00:16
Jan 31 12:00:16 test-1 automount[3432]: bind_ldap_simple: lookup(ldap): Unable to bind to the LDAP server: (default), error Can't contact LDAP server
Jan 31 12:00:16 test-2 automount[3544]: bind_ldap_simple: lookup(ldap): Unable to bind to the LDAP server: (default), error Can't contact LDAP server
Jan 31 12:00:16 hsv-ch02 automount[3006]: bind_ldap_simple: lookup(ldap): Unable to bind to the LDAP server: (default), error Can't contact LDAP server

The way it don't works as Below: 它不起作用的方式如下:

$ cat Search_string_input.py
#!/usr/bin/python3
import re
File = input("Please Enter the File Name : ")
Patt_Search = input("Please Enter the Pattern you want to search : ")
ldap = open(File, mode='rt', encoding='utf-8')
for line in ldap:
    line = line.rstrip()
    #if re.search('12:00:16', line) :
    if re.search('Patt_Search', line) :
        print(line)

Result is as below, which don't yeild any output and no error though! 结果如下,它没有产生任何输出,但是也没有错误!

$ ./Search_string_input.py
Please Enter the File Name : ldap_data
Please Enter the Pattren you want to serach : 06:10:13

If you're using 'Patt_Search' , it just searches for the string literal 'Patt_Search' . 如果您使用'Patt_Search''Patt_Search''Patt_Search'搜索字符串文字'Patt_Search' A string containing a variable name has nothing to do with the variable itself. 包含变量名的字符串与变量本身无关。

  • in the first case you're passing your input 12:00:16 , which is found three times in the file and printed to the screen. 在第一种情况下,您将传递您的输入12:00:16 ,该输入在文件中被发现三次并打印到屏幕上。

  • in the second case you're searching for the string Patt_Search , which can't be found in the file ldap_data , thus nothing gets printed to the screen. 在第二种情况下,您正在搜索字符串Patt_Search ,该字符串在文件ldap_data ,因此屏幕上没有任何内容。

https://docs.python.org/3/library/re.html https://docs.python.org/3/library/re.html

What is the purpose of this question? 这个问题的目的是什么? The first script apparently works as expected, so why not just use that? 第一个脚本显然可以按预期工作,所以为什么不使用它呢?

In the second script, 'Patt_Search' is a literal string, so re.search('Patt_Search', line) searches for the string Patt_Search in the input line. 在第二个脚本中, 'Patt_Search'是文字字符串,因此re.search('Patt_Search', line)在输入行中搜索字符串Patt_Search I'm guessing that your LDAP data file does not contain the string Patt_Search anywhere, hence there will not be any matches, and there will be no output nor any errors. 我猜想您的LDAP数据文件在任何地方都不包含字符串Patt_Search ,因此将没有任何匹配项,也将没有输出也没有任何错误。

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

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