New here, trying to learn regex -

General questions about using TextPad

Moderators: AmigoJack, bbadmin, helios, Bob Hansen, MudGuard

Post Reply
wjb
Posts: 11
Joined: Fri Apr 28, 2006 3:01 pm
Contact:

New here, trying to learn regex -

Post by wjb »

I've read thru help and some tutorials but still struggling.
I am just testing different expressions, but sometimes I
cant see why they don't work. I'm just going to start asking

When I use this regex
^[a-z]*

I expected to find the start of a line and the first portion of that line containing letters a-z.

Instead I get nothing. Does the * not work after the []?

Bill
User avatar
Bob Hansen
Posts: 1516
Joined: Sun Mar 02, 2003 8:15 pm
Location: Salem, NH
Contact:

Post by Bob Hansen »

Does it work when you put a check mark in the box for "Regular expression" ?
Make sure there is no checkmark in "Match case" ?

If that does not work, then:
Are you getting back an error message? What does it say?

Provide a sample of the text you are searching.
Hope this was helpful.............good luck,
Bob
gan

Post by gan »

This seems like a bug to me. The expression work fine on my computer as long as i start the search when the cursor is on the beginning of a line that start with a letter. When i start the search or click find next when the cursor is on the beginning of a line that do not start with a letter or is empty it won't continue to search the rest of the lines below even if they match it seems.

For example using this lines:

Code: Select all

5555
aa22
When the cursor is at the beginning of line 2 or in the middle of line 1 when i search using the expression ^[a-z]+ i get a match on "aa", but if the cursor is on the beginning of line 1 when i start the search i get no match, error or messages......basically nothing happen when i click the find button. Seems like textpad behave a bit strange compared to other text editors using regular expression with the "*".

Anyway wjb i believe the the expression you really want to use in this case to get the result you want is a "+" instead of a "*" (Like this: "^[a-z]+").
bveldkamp

Post by bveldkamp »

Check "Wrap searches" or make sure "Direction: Down" is selected
agnul
Posts: 21
Joined: Tue Feb 10, 2004 9:42 am
Location: Italy

Post by agnul »

That regular expression should match every single line in any file (unless match case is checked) since all it says is "match the lines that begin with zero or more occurences of any (lowercase) letter". If a line begins with a lowercase letter the regex matches (1 occurrence), if it doesn't the regex will match anyway (0 occurrences).

What's strange is that it wont match any more lines after matching one not beginning with a letter (tested with 5.03). OTOH clicking "mark all" correctly marks every line...
User avatar
MudGuard
Posts: 1295
Joined: Sun Mar 02, 2003 10:15 pm
Location: Munich, Germany
Contact:

Post by MudGuard »

agnul wrote:What's strange is that it wont match any more lines after matching one not beginning with a letter (tested with 5.03). OTOH clicking "mark all" correctly marks every line...
Once it has matched the nothingness at the beginning of the first line, it starts for matching the next thing immediately after the first match - i.e. at the beginning of the first line.
There the condition that the match must start at the beginning of the line is still true, and the beginning of the line is still followed by nothing (or more).
Therefore the second match happens at exactly the same place as the first one.
And the third, fourth, fivth, ... also.

For the Mark All, Textpad probably marks the line of the first hit and starts at the next line as for marking all matching lines it does not matter if there is just one or infinitely many matches in this line - once the line matches and gets marked it is no longer of interest.
gan

Post by gan »

As far as i understand searching for "^[a-z]*" would be a wrong way of using the regular expressions anyway since it will match every line. I cannot see how that could be useful, but still "find next" shouldn't find the same occurrence without moving to the next.
I use another text editor as well and seems to work as expected using that editor so "find next" find the next occurrence and not the same over and over again. Not a big deal though since searching for "^[a-z]*" isn't really useful.
bveldkamp

Post by bveldkamp »

It should match every line, TP however stops when it hits an empty line, or a line that doesn't start with a-z.
I tried some other editors:
NP++ shows the same behaviour as TP
VS2003/2005/2008 find every line
NetBeans highlights every line starting with a-z
gan wrote:As far as i understand searching for "^[a-z]*" would be a wrong way of using the regular expressions anyway since it will match every line.
gan

Post by gan »

Ultraedit match every line as well, but doesn't stop on a empty line or a line that does not start with a letter. Ultraedit will continue until the end of the file.
agnul
Posts: 21
Joined: Tue Feb 10, 2004 9:42 am
Location: Italy

Post by agnul »

MudGuard wrote:Once it has matched the nothingness at the beginning of the first line, it starts for matching the next thing immediately after the first match - i.e. at the beginning of the first line.
I knew there had to be a perfectly obvious reason I'd fail to think of ;-)
wjb
Posts: 11
Joined: Fri Apr 28, 2006 3:01 pm
Contact:

Post by wjb »

Thanks for the replies

I know it not a useful search, I am just trying to learn.

Glad to see it it should be working, I thought I was missing something. The
blank line appears to be the problem.

Don't know why using the ^[a-z]+ gets by a blank line but ^[a-z]* does not.
User avatar
MudGuard
Posts: 1295
Joined: Sun Mar 02, 2003 10:15 pm
Location: Munich, Germany
Contact:

Post by MudGuard »

wjb wrote: Don't know why using the ^[a-z]+ gets by a blank line but ^[a-z]* does not.
Because [a-z]+ matches at least one character.
Thus after each match, the start position for the search of the next match is at a different position. Thus no endless loop of matching at the same position can occur.
Post Reply