? Need Help with Regular Expression

Just starting out? Need help? Post your questions and find answers here.
millie78526
User
User
Posts: 23
Joined: Thu Apr 18, 2024 9:12 pm

? Need Help with Regular Expression

Post by millie78526 »

TIA ;
I want to weed out lines that have only numeric characters OR
numeric characters and space(s) .
If it is a numeric line , I want to 'Goto SkipThisLine'
But my RegEx lets thru every line .
How can this be fixed?

Code: Select all

;    CreateRegularExpression(0, "^[0-9 ]+$") ;  First Try "^[0-9]+$" 
     If ExamineRegularExpression(0, aLine$)
       Debug "RegularExpressionError() = " + RegularExpressionError()
;        Goto SkipThisLine      
       Debug  "SkipThisLine = " +  aLine$
     EndIf
       Debug  "This Line Not Numeric = " +  aLine$
Andesdaf
User
User
Posts: 83
Joined: Sun Mar 22, 2009 2:53 pm
Location: GER, Saxony

Re: ? Need Help with Regular Expression

Post by Andesdaf »

Use MatchRegularExpression() instead
User avatar
jacdelad
Addict
Addict
Posts: 1991
Joined: Wed Feb 03, 2021 12:46 pm
Location: Riesa

Re: ? Need Help with Regular Expression

Post by jacdelad »

If you use ExamineRegularExpression, it will always return true, as long as the the examination can be started. You need NextRegularExpressionMatch, to find the next match. This is helpful to find the lines, when you want to match a textblock with more than one line. To weed out the numeric lines, you need to negate the RegEx and put brackets around the match to read it.
When comparing it line by line, use MatchRegularExpression, like Andesdaf said. This is a matter of speed, if you want to compare a lot of lines which come in a block (like reading a log file) I would prefer the first method, which should be faster.
Good morning, that's a nice tnetennba!

PureBasic 6.21/Windows 11 x64/Ryzen 7900X/32GB RAM/3TB SSD
Synology DS1821+/DX517, 130.9TB+50.8TB+2TB SSD
AZJIO
Addict
Addict
Posts: 2141
Joined: Sun May 14, 2017 1:48 am

Re: ? Need Help with Regular Expression

Post by AZJIO »

https://www.purebasic.fr/english/viewtopic.php?t=76808
click the "Copy" button and you will get ready-made code for the regular expression
millie78526
User
User
Posts: 23
Joined: Thu Apr 18, 2024 9:12 pm

Re: ? Need Help with Regular Expression

Post by millie78526 »

Thank you Andesdaf , works much better:

Code: Select all

;    CreateRegularExpression(0, "^[0-9 .]+$") ; First Try "^[0-9]+$" 
    If MatchRegularExpression(0, aLine$)  
;       Debug "RegularExpressionError() = " + RegularExpressionError()
       Debug  "SkipThisLine = " +  aLine$
        Goto SkipThisLine      
     EndIf
Post Reply