Note that there are some explanatory texts on larger screens.

plurals
  1. POI'm using a straight forward regex pattern ('\s+') that is not ignoring white space
    primarykey
    data
    text
    <p>I'm not sure why this regex pattern ('\s+') is not ignoring whitespace: the code below searches all of the .txt and .log files in a directory and returns the matched string entered by the user. It takes the string and converts it into hex and ASCII, then concurrently searches all .txt and .log files for the string, hex, and ASCII match. I entered the values of a converted string in 3 different .txt files: string in one, hex in another, and ascii in the third. Initially, all files were matched. However, I added the <code>regex.search(re.sub(r'\s+', '', line))</code> below in the second major if statement, and went into the .txt file where the string converted to ASCII was entered, and I added a space in the string. I then attempted another search with the same string and only found two matches: string and hex. The search "ignoring whitespace" did not match the altered ASCII string. Am I overlooking or doing something wrong?</p> <p>Enter string: Rozelle07 (matched) Hex conversion: 526f7a656c6c653037 (matched) ascii conversion: 821111221011081081014855 (matched)</p> <p>Altered ascii string: 8211112210110810810148 55 (regexp did not match when I tried this).</p> <pre><code> print " Directory to be searched: c:\Python27 " directory = os.path.join("c:\\","SQA_log") userstring = raw_input("Enter a string name to search: ") userStrHEX = userstring.encode('hex') userStrASCII = ''.join(str(ord(char)) for char in userstring) regex = re.compile(r"(%s|%s|%s)" % ( re.escape( userstring ), re.escape( userStrHEX ), re.escape( userStrASCII ))) choice = raw_input("Type 1: search with respect to whitespace. Type 2: search ignoring whitespace: ") if choice == '1': for root,dirname, files in os.walk(directory): for file in files: if file.endswith(".log") or file.endswith(".txt"): f=open(os.path.join(root, file)) for i,line in enumerate(f.readlines()): result = regex.search(line) if regex.search(line): print " " print "Line: " + str(i) print "File: " + os.path.join(root,file) print "String Type: " + result.group() print " " f.close() re.purge() if choice == '2': for root,dirname, files in os.walk(directory): for file in files: if file.endswith(".log") or file.endswith(".txt"): f=open(os.path.join(root, file)) for i,line in enumerate(f.readlines()): result = regex.search(re.sub(r'\s+', '',line)) if regex.search(line): print " " print "Line: " + str(i) print "File: " + os.path.join(root,file) print "String Type: " + result.group() print " " f.close() </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload