Jump to content

Help to match whitespace not between quotes -- StringRegExpReplace


 Share

Recommended Posts

I have been pulling my hair out trying to come up with a regular expression that will match all whitespace not between quote characters in a string using StringRegExpReplace. Does anyone know how to even begin to make a pattern to do this? I haven't worked with regular expressions in years, but I know it would be the most efficient way to accomplish my task.

Link to comment
Share on other sites

I want to match and replace any whitespace with a null string found in a sting that is not between quotes. The goal is to be able to check lines of script files and remove all unnecessary white space from the line. The goal is to reduce overall file size of the various scripts and applications i have created for publishing on the web. Since most scripting and programming languages define strings by delimiting and terminating the string with quote characters, removing white space from the actual string of a line would be bad.

Examples:

$aStr = "Some sentence of text that forms a string of text"

Result after StringRegExpReplace

$aStr="Some sentence of text that forms a string of text"

WinSetTrans   ("Title of windows",      "Some window text", 55)

Result after StringRegExpReplace

WinSetTrans("Title of windows","Some window text",55)

I want to come up with a regular expression to use to accomplish this with StringRegExpReplace. The algorithm I am using currently seems way to inefficient. However, I can't seem to figure out the proper pattern to accomplish this.

Any regular expression experts out there, your help would be greatly appreciated.

Link to comment
Share on other sites

don't have the time to help the second one, but I hope this example will help you guys in any matter, I hope I understood the first guy issue because it was not so well expalined, never the less here is the way to do this: changing regular white spaces with ":" and under qoutes with "~"

#include <Array.au3>
#include <String.au3>
Dim $E[40]
;changing regular white spaces with ":" and under qoutes with "~"
$m = "hi this is me and I am checking" & " """ & " """ & " that it finds white spaces Not between qoutes" & " """ & " """ & " hope it can find it and change them"
msgbox (0,"$m before",$m)
$p = $m
$pp = 0
While 1
    $z = StringInStr($p, " ", 1)
    If $z = 0 Then ExitLoop
    $p = StringTrimLeft($p, $z)
;MsgBox (0,"this is $p: after",$p)
    $pp = $pp + 1
WEnd
;MsgBox (0,"this is $pp",$pp)
For $o = 1 To $pp
    $p1 = StringInStr($m, Chr(32))
    $p2 = StringMid($m, 1, $p1 + 1)
;MsgBox (0,"this is $p2:",$p2 & $p1)
    $n = StringRegExp($p2, """ """)
;MsgBox(0, "this is $n", $n)

    If $n = 0 Then
        $m = StringReplace($m, $p1, ":")
    Else
        $m = StringReplace($m, $p1, "~")
    EndIf
    $E[$o] = $m
;MsgBox(0, "this is $m", $m)
Next


MsgBox(0, "this is $m at the end of the process:", $m)
Edited by erezlevi
Link to comment
Share on other sites

I want to match and replace any whitespace with a null string found in a sting that is not between quotes. The goal is to be able to check lines of script files and remove all unnecessary white space from the line. The goal is to reduce overall file size of the various scripts and applications i have created for publishing on the web. Since most scripting and programming languages define strings by delimiting and terminating the string with quote characters, removing white space from the actual string of a line would be bad.

Examples:

$aStr = "Some sentence of text that forms a string of text"

Result after StringRegExpReplace

$aStr="Some sentence of text that forms a string of text"

WinSetTrans   ("Title of windows",      "Some window text", 55)

Result after StringRegExpReplace

WinSetTrans("Title of windows","Some window text",55)

I want to come up with a regular expression to use to accomplish this with StringRegExpReplace. The algorithm I am using currently seems way to inefficient. However, I can't seem to figure out the proper pattern to accomplish this.

Any regular expression experts out there, your help would be greatly appreciated.

if you are using AUTOIT then use ctrl+T for Tidy, it will do what you want for AutoIT scripts only!

also, I think that every case needs a different expression, well I can solve your problem this way which is easy:

$m="WinSetTrans   (""Title of windows"",       ""Some window text"", 55)"

$p=StringRegExpReplace ($m,"  ","")
MsgBox (0,"this is $p",$p)

all none-necesary spaces are gone. but, look even when Tidy was written I think Tidy has at least 20-30 different regular expressions, because while, For, If and many more have different "looks" in AutoIT then one another.

Edited by erezlevi
Link to comment
Share on other sites

I want to match and replace any whitespace with a null string found in a sting that is not between quotes. The goal is to be able to check lines of script files and remove all unnecessary white space from the line. The goal is to reduce overall file size of the various scripts and applications i have created for publishing on the web. Since most scripting and programming languages define strings by delimiting and terminating the string with quote characters, removing white space from the actual string of a line would be bad.

Examples:

$aStr = "Some sentence of text that forms a string of text"

Result after StringRegExpReplace

$aStr="Some sentence of text that forms a string of text"

WinSetTrans   ("Title of windows",      "Some window text", 55)

Result after StringRegExpReplace

WinSetTrans("Title of windows","Some window text",55)

I want to come up with a regular expression to use to accomplish this with StringRegExpReplace. The algorithm I am using currently seems way to inefficient. However, I can't seem to figure out the proper pattern to accomplish this.

Any regular expression experts out there, your help would be greatly appreciated.

I found solving this problem very addictive :) It taught me that I still have a great deal to learn about regular expressions. I've tested it on several different variations of your example text and it works for me. It may not be fully optimised but I'll leave that for another day or someone else.

$sRegExPattern = '((?!("[^"]*))( +)+(?=[^"]*"\B)|(?!\b"[^"]*)( +)+(?=([^"]*)$)|(?=\b"[^"]*)( +)+(?=[^"]*"\B))'

$sExample1 = 'Some sentence of text that forms a string of text'
ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $sExample1 = ' & $sExample1 & @crlf & '>Error code: ' & @error & @crlf)
$sResult1 =StringRegExpReplace($sExample1,$sRegExPattern,'')
ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $sResult1  = ' & $sExample1 & @crlf & '>Error code: ' & @error & "|" & @extended & @crlf)

$sExample2 = 'WinSetTrans   ("Title of windows",         "Some window text", 55)'
ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $sExample2 = ' & $sExample2 & @crlf & '>Error code: ' & @error & @crlf) 
$sResult2 = StringRegExpReplace($sExample2,$sRegExPattern,'')
ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $sResult2  = ' & $sResult2 & @crlf & '>Error code: ' & @error & "|" & @extended & @crlf)

Explaination of this regular expression attached

Edited by Bowmore

"Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to build bigger and better idiots. So far, the universe is winning."- Rick Cook

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...