I am trying to develop a tool which will be a vbs file to compress a page of code.
I need help with regular expression to trim leading white space off of each line of code as it is read from the file. I can read the file and render it back to the command result, but cannot seem to trim leading whitespace off of each line.
I tried simple vbs "trim" but this apparently doesn't recognize the TextPad whitespace.
can anyone assist in developing a re pattern or solution, given that there will be multiple text words in each line of code ?
remove leading whitespace from line of code
Moderators: AmigoJack, bbadmin, helios, Bob Hansen, MudGuard
- webber123456
- Posts: 50
- Joined: Wed Jul 30, 2003 2:55 am
- Bob Hansen
- Posts: 1516
- Joined: Sun Mar 02, 2003 8:15 pm
- Location: Salem, NH
- Contact:
Hello Webber1-6:
To remove any leading Space, tab, vertical tab or form feed characters, try this:
1. Go to top of document
2. Open Search, Replace
3. Set Conditions for Text and Regular Expressions. Set Scope for Active Document
3. Search for: ^[: :]*
Replace with: (leave blank)
4. Click on Replace All. Click on Close to close the Replace Window.
5. DONE!.........
==============================
Explanation of RegEx:
^ is Start at beginning of the line
[: :] is a space character (a [, a colon, a space, another colon, a ]).
* is any quantity of the preceeding character (space)
===============================
Groups of spaces within the line will remain.
You can test the Replace process on the following lines: (I had to insert periods to represent spaces on this web site, so replace periods after you copy and paste into TextPad).
....................line 1 is extended
...............................line 2 is extended further
..........line 3 is extended less than line 1
line 4 is not extneded at all
..........line 5............is extended and then continued........... after some more spaces
=============================
Good practice to make sure you have a backup before doing Replace actions, and/or be prepared to do Edit, Undo if there is a problem.
Hope this helps..................good luck,
Bob
To remove any leading Space, tab, vertical tab or form feed characters, try this:
1. Go to top of document
2. Open Search, Replace
3. Set Conditions for Text and Regular Expressions. Set Scope for Active Document
3. Search for: ^[: :]*
Replace with: (leave blank)
4. Click on Replace All. Click on Close to close the Replace Window.
5. DONE!.........
==============================
Explanation of RegEx:
^ is Start at beginning of the line
[: :] is a space character (a [, a colon, a space, another colon, a ]).
* is any quantity of the preceeding character (space)
===============================
Groups of spaces within the line will remain.
You can test the Replace process on the following lines: (I had to insert periods to represent spaces on this web site, so replace periods after you copy and paste into TextPad).
....................line 1 is extended
...............................line 2 is extended further
..........line 3 is extended less than line 1
line 4 is not extneded at all
..........line 5............is extended and then continued........... after some more spaces
=============================
Good practice to make sure you have a backup before doing Replace actions, and/or be prepared to do Edit, Undo if there is a problem.
Hope this helps..................good luck,
Bob
Bob.. is webber trying to remove the whitespace in TP?? from what i understand in the post he's reading in the lines from wherever and want to remove the space before it goes back out... (i could be wrong)
Webber, if i understand correctly then the regex will be
s/^\s+//;
simply meaning remove all spaces, tabs or anything thats not a character and repeat as long as we dont hit anything solid, then stop.... the // at the end means replace with nothing; this is what you will use in a script, use bob's regex in TP.
If you wanted to replace each space with a Q, you would use s/^\s+/Q/; this is to explain why theres nothing between the two //.
To remove continuous space or tabs within the body of each line will call for another step to the regex...
Webber, if i understand correctly then the regex will be
s/^\s+//;
simply meaning remove all spaces, tabs or anything thats not a character and repeat as long as we dont hit anything solid, then stop.... the // at the end means replace with nothing; this is what you will use in a script, use bob's regex in TP.
If you wanted to replace each space with a Q, you would use s/^\s+/Q/; this is to explain why theres nothing between the two //.
To remove continuous space or tabs within the body of each line will call for another step to the regex...
- webber123456
- Posts: 50
- Joined: Wed Jul 30, 2003 2:55 am
ok webber, that regex i gave you before has the replace or substitue function already within.. that wouldnt work for vbs (now i see ure code), that would work for perl
s/^\s+//;
the first s means substitute anything matching the pattern between the first / / (which is ^\s+) with what ever is between the second // (which is nothing)
however that is a perl built in method without all the long regex construct required for vbs
for vbs you would try this
---------------------------------------------------------
Dim objRegExp
Set objRegExp = New Regexp
objRegExp.IgnoreCase = True
objRegExp.Global = True
objRegExp.Pattern = "^\s+"
CurrentLine = objRegExp.Replace(CurrentLine, "")
-----------------------------------------------------------
in this example vbs will capture the pattern which says :
look for anything thats not a character (\s)
at the start of the line (^)
and continue until something solid turns up (+) which means match one or more times
CurrentLine should hold the value of the current line in the loop..
The replace function will replace everything in CurrentLine that matches your regex with "" which you already know means nothing..
should work ....
s/^\s+//;
the first s means substitute anything matching the pattern between the first / / (which is ^\s+) with what ever is between the second // (which is nothing)
however that is a perl built in method without all the long regex construct required for vbs
for vbs you would try this
---------------------------------------------------------
Dim objRegExp
Set objRegExp = New Regexp
objRegExp.IgnoreCase = True
objRegExp.Global = True
objRegExp.Pattern = "^\s+"
CurrentLine = objRegExp.Replace(CurrentLine, "")
-----------------------------------------------------------
in this example vbs will capture the pattern which says :
look for anything thats not a character (\s)
at the start of the line (^)
and continue until something solid turns up (+) which means match one or more times
CurrentLine should hold the value of the current line in the loop..
The replace function will replace everything in CurrentLine that matches your regex with "" which you already know means nothing..
should work ....
- webber123456
- Posts: 50
- Joined: Wed Jul 30, 2003 2:55 am