Page 1 of 1
RegularExpressions
Posted: Wed Dec 10, 2008 12:46 pm
by naw
Firstly well done for adding Unix style Regular Expressions in PB, potentially very useful since Regular Expressions are v-e-r-y powerful.
So - anyways - here is my wishlist:
1) Could we perhaps shorten/truncate the commands since
Code: Select all
CreateRegularExpression() / MatchRegularExpression()
is a bit of a mouthful - perhaps to
2) MatchRegularExpression() currently only returns 0 or 1 - it would be more useful to return the position of the first matched string - eg:
Code: Select all
CreateRegExpr(0,"[A,B,C]")
debug MatchRegExpr(0,"abcABCDABCD"); Returns 4
3) Knowing the number of matched Regular Expressions would be really useful too:
Code: Select all
CreateRegExpr(0,"[A,B,C]")
debug CountRegExpr(0,"abcABCDABCD"); Returns 6
To make best use of this powerful library we need new commands to:
4) Search & replace and return the position of matched Regular Expressoins - eg:
Code: Select all
CreateRegExpr(0, "[A-Z]")
debug ReplaceRegExpr(0,"Hello world","`"); Returns "`ello world"
5) Transpose strings using Regular Expressions (like the Unix 'tr' command - eg:
Code: Select all
CreateRegExpr(0, "[A-Z]");CreateRegExpr(1, "[a-z]");
TransposeRegExpr(0,1,"Hello World"); Returns "hello world"
Posted: Wed Dec 10, 2008 12:57 pm
by eddy
Regex term is often used.
Re: RegularExpressions
Posted: Wed Dec 10, 2008 1:05 pm
by Progi1984
naw wrote:1) Could we perhaps shorten/truncate the commands since
Code: Select all
CreateRegularExpression() / MatchRegularExpression()
is a bit of a mouthful - perhaps to
For this point, a macro do this :
Code: Select all
Macro CreateRegExpr
CreateRegularExpression
EndMacro
Macro MatchRegExpr
MatchRegularExpression
EndMacro
Re: RegularExpressions
Posted: Wed Dec 10, 2008 3:14 pm
by AND51
> Could we perhaps shorten/truncate the commands since
I use auto-completetition, so there's o problem with long commands. Remember that PureBasic must stay easy/readable also fpr beginners. You can also use macros, as Progi1984 said.
> MatchRegularExpression() currently only returns 0 or 1 - it would be more useful to return the position of the first matched string
I already asked this, I even asked Fred this via PM and he will NOT implement that, because of performance-reasons.
But I agree, it would be very very very good, if we had an additional command for that!
>Knowing the number of matched Regular Expressions would be really useful too
I also asked Fred this. His solution is to misuse ExtractRegularString(), as it returns the number of results.
@ Fred: I find this a bit too ressource-consuming as you need an additional array which costs memory and time.
> Search & replace and return the position of matched Regular Expressoins
Do I misunderstand you? There is already such a command: ReplaceRegularExpression(). The only thing that must be implemented is that ReplaceString$ can be a RegExp, too.
> the Unix 'tr' command
I only used 'tr' once, but IMHO you can do this already with ReplaceRegularExpression(). Doesn't 'tr' only handles single characters? If so, can't you use ReplaceRegularExpression(0, "Hello World", "[a-z]") or do I have a thinking-error?
// Edit:
5) Well this is a good example for ReplaceRegularExpressions() to support RegExp.
Posted: Wed Dec 10, 2008 8:02 pm
by AND51
@ Fred:
I just got the following idea:
ExtractRegularExpression() redims thegiven Array, so it has to know the number of results, hasn't it? If so, there must be a kind of CountRegularExpressionMatches()...
Or does ExtractRegularExpressionError() redim the array each time an element is added?
Posted: Thu Dec 11, 2008 1:08 pm
by naw
@AND51 & progi1984,
re: xxxxxRegExpr() - I'm an ex Unix admin, so I just like my commands to be short and cryptic eg: sed, awk, grep, tr, bc
re: MatchRegularExpression() counts the number of matches - Sorry - I guess I should have (re)searched the forums first - but still the RE is incredibly powerful, but we need the tools to exploit it
re: ReplaceRegularExpression() - yes would be great to use RE for the ReplaceString - just like the tr command eg:
Code: Select all
echo "aBcDeFgH" | tr "[a-z]" "[A-Z]" #returns ABCDEFGH
Generally the RegExpr library implementation is a bit cumbersome (IMO).
I dont understand why
CreateRegularExpression() &
FreeRegularExpression() have been implemented - presumably there must be some advantage in pre-loading RE's into an array, but I cant imagine what or why?
Things would be much neater if we could replace:
Code: Select all
CreateRegularExpression(0, "[A-Z][A-Z,0-9][0-9] [0-9][A-Z][A-Z]")
Debug MatchRegularExpression(0, "S43 2BS") ; Will be 1
Debug MatchRegularExpression(0, "NG9 2BS") ; Will be 1
Debug MatchRegularExpression(0, "EC14 2BS") ; Will be 1
FreeRegularExpression(0)
with:
Code: Select all
Debug MatchRegularExpression(PCODE.s, "S43 2BS")
Debug MatchRegularExpression("[A-Z][A-Z,0-9][0-9] [0-9][A-Z][A-Z]", "NG9 2BS")
Debug MatchRegularExpression("[A-Z,A-Z,0-9,0-9]", "EC14 2BS")