Page 1 of 1
Help regexp
Posted: Sun Dec 10, 2023 10:34 am
by loulou2522
Hello,
Starting string
string.s="FL 565 860 500"
but perhaps also
string.s="FL 565 860 "
or
string.s="FL 500"
I would like to split the following regexp into two parts using the following pattern:
(
\d{0,3}\s{0,}\d{1,3}| (\d{0}\s{0,}\d{0}))*
but the result I get is not what I expected
Result obtained
565 860 500
expected result
565 850
500
Can anyone help me solve this problem with the correct regex?
Thanks in advance
Re: Help regexp
Posted: Sun Dec 10, 2023 11:28 am
by AZJIO
Here is a topic on regular expressions. High chance that they will help.
I can only show a few mistakes
\s{0,} = \s*
\d{0} - This doesn’t happen, just delete this text and it will be the same. If the number does not exist, then it does not need to be written
Re: Help regexp
Posted: Sun Dec 10, 2023 1:00 pm
by infratec
Why an extra library (>100kB) for such an easy thing?
Code: Select all
EnableExplicit
Structure Split_Structure
First$
Second$
EndStructure
Procedure StringSplit(String$, *Split.Split_Structure)
Protected i.i, Part$
If Left(string$, 2) = "FL"
If CountString(string$, " ") = 1
*Split\Second$ = StringField(string$, 2, " ")
Else
*Split\First$ = StringField(string$, 2, " ") + " " + StringField(string$, 3, " ")
*Split\Second$ = StringField(string$, 4, " ")
EndIf
EndIf
EndProcedure
Define string$, Split.Split_Structure
Debug "---"
string$ = "FL 565 860 500"
StringSplit(string$, @Split)
Debug Split\First$
Debug Split\Second$
Debug "---"
Split\First$ = ""
Split\Second$ = ""
string$ = "FL 565 860 "
StringSplit(string$, @Split)
Debug Split\First$
Debug Split\Second$
Debug "---"
Split\First$ = ""
Split\Second$ = ""
string$ = "FL 500"
StringSplit(string$, @Split)
Debug Split\First$
Debug Split\Second$
Re: Help regexp
Posted: Sun Dec 10, 2023 4:08 pm
by loulou2522
Thanks infratec,
Burt the main problem is that i must use an regexp because i dont know where is the position of the first occurence neither than the position of the second occurance thar can be not at the same place in different string
example
Var1="FL 500 1 500"
or var1 = "FL 500 1 500"
or simply
var = "FL 500"
in eachh case i have to create two variable
500 and 1500
my regex
\d{0,3}\s{0,}\d{1,3}| (\d{0}\s{0,}\d{0})) that doen't work
Re: Help regexp
Posted: Sun Dec 10, 2023 4:59 pm
by ChrisR
If I take your examples:
1) string.s="FL 565 860 500" ==> 565860 and 500
2) string.s="FL 500 1 500" ==> 500 and 1500
Why for case 1, it is 565860 and 500 and not 565 and 860500
Same for case 2, why it is 500 and 1500 and not 5001 and 500
What is the exact rule ?
Re: Help regexp
Posted: Sun Dec 10, 2023 5:17 pm
by Marc56us
The search string begins with FL followed by 1 to 3 numbers, the second of which may contain a space.
If different, please state more precisely
Code: Select all
EnableExplicit
; RegEx cut to make it easier to understand :-)
#RegEx = "FL\h+(\d+)" +
"(?:\h+(\d\h?\d+))?" +
"(?:\h+(\d+))?"
Define Txt$ = ~"string.s=\"FL 565 860 500\"" +
"but perhaps also" +
~"string.s=\"FL 565 860 \"" +
"Or" +
~"string.s=\"FL 500\"" +
~"Var1=\"FL 500 1 500 »" +
"ou var1 = « FL 500 1 500 »" +
"ou tout simplement" +
"var = « FL 500 »" +
"deux variables" +
"500 et 1500"
If Not CreateRegularExpression(0, #RegEx) : Debug "Bad ReGex" : End : EndIf
If Not ExamineRegularExpression(0, Txt$) : Debug "No match" : End : EndIf
Define A$, B$, C$
While NextRegularExpressionMatch(0)
A$ = RegularExpressionGroup(0, 1)
B$ = RemoveString(RegularExpressionGroup(0, 2), " ")
C$ = RegularExpressionGroup(0, 3)
Debug A$ + " " + B$
Debug C$
Debug "-----------------------------------"
Wend
FreeRegularExpression(0)
End
If any number can contain spaces, this can also, but differently
Are numbers greater than 999 also less than 100,000?
Is this data for converting flight plans?: FL : Flight Level ? (in which case it's easier)
To make things easier for us, please provide us with a list of the lines that must match and a list of those that must not.
Re: Help regexp
Posted: Sun Dec 10, 2023 5:57 pm
by normeus
If you really want to use Reg Ex then there is nothing like Didelphodon's RexMan to test your PureBasic code:
https://www.purebasic.fr/english/viewto ... 39#p284139
Otherwise, a real set of rules for the data or about 20 lines of sample data to create a usable regex. I say 20 because that will take some time for you to type. Having 2 sample data lines is not enough in my humble opinion.
Norm.
Re: Help regexp
Posted: Sun Dec 10, 2023 7:53 pm
by loulou2522
In this case
txt$= "FL 100 500 100 500"
var1 must contain 100500
and var2 must contain 10500
Re: Help regexp
Posted: Mon Dec 11, 2023 4:07 am
by AZJIO
https://www.purebasic.fr/english/viewto ... 86#p611986
Code: Select all
#RegExp = 0
Text$ = "FL 100 500 1 500"
num$ = "(\d{3}[\h;.]\d{3} | \d{1,2}[\h;.]\d{3} | \d[\h;.]\d{3}[\h;.]\d{3})"
word$ = "([a-z]{2})"
RegExp$ = ""
RegExp$ + word$
RegExp$ + "\h+"
RegExp$ + num$
RegExp$ + "\h+"
RegExp$ + num$
RegExp$ + "\h*"
RegExp$ + num$ + "?"
; Debug RegExp$
If CreateRegularExpression(#RegExp, RegExp$, #PB_RegularExpression_Extended | #PB_RegularExpression_NoCase)
Groups = CountRegularExpressionGroups(#RegExp)
If Not Groups
Debug 0
EndIf
If ExamineRegularExpression(#RegExp, Text$)
While NextRegularExpressionMatch(#RegExp)
For i = 1 To Groups
If i > 1
Debug ReplaceString(RegularExpressionGroup(#RegExp, i), " ", "") ; There may be a regular expression for [\h;.]
Else
Debug RegularExpressionGroup(#RegExp, i)
EndIf
Next
Wend
EndIf
Else
Debug RegularExpressionError()
EndIf
Re: Help regexp
Posted: Mon Dec 11, 2023 7:50 am
by infratec
The main point is:
If you don't specify all cases, there is no solution for all cases.
So I invested time for 'nothing', because you did not tell the whole truth.
This is very frustrating.
So please : all or nothing, but not only 2 cases from many cases.
Re: Help regexp
Posted: Mon Dec 11, 2023 8:07 am
by AZJIO
I think that a list of data is being tested and if something does not fit into it, then we are offered a new list element that failed the test. This reminds me of cleaning HTML code from garbage saved using "ms office word", each time new code elements are found that are garbage and I have to complicate the regular expression. This takes forever because only the developers know what tags they can add to the code when interpreting the content in the document.
The only way out is that instead of issuing a ready-made regular expression, you need to teach how to create it yourself.
By the way, I have long wanted to create a topic with a request to add regular expression rules to the CreateRegularExpression() help page or in this section to provide a link to the help page about regular expression rules. The official PCRE engine
help page has a lot of text to read, so it's best to create simplified help content.
This is what it looks like in AutoIt3
Re: Help regexp
Posted: Mon Dec 11, 2023 9:11 am
by Marc56us
loulou2522 wrote: Sun Dec 10, 2023 7:53 pm
In this case
txt$= "FL 100 500 100 500"
var1 must contain 100500
and var2 must contain 10500
If the field separator (space) and the thousand separator (space) are identical, and the number of variables is not constant (2 ?, 3 ?, 4 ?), then automatic data analysis is not possible.