Help regexp

Just starting out? Need help? Post your questions and find answers here.
loulou2522
Enthusiast
Enthusiast
Posts: 488
Joined: Tue Oct 14, 2014 12:09 pm

Help regexp

Post 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
AZJIO
Addict
Addict
Posts: 1257
Joined: Sun May 14, 2017 1:48 am

Re: Help regexp

Post 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
infratec
Always Here
Always Here
Posts: 6786
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Help regexp

Post 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$
loulou2522
Enthusiast
Enthusiast
Posts: 488
Joined: Tue Oct 14, 2014 12:09 pm

Re: Help regexp

Post 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
User avatar
ChrisR
Addict
Addict
Posts: 1098
Joined: Sun Jan 08, 2017 10:27 pm
Location: France

Re: Help regexp

Post 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 ?
Marc56us
Addict
Addict
Posts: 1477
Joined: Sat Feb 08, 2014 3:26 pm

Re: Help regexp

Post 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 :wink:

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.
normeus
Enthusiast
Enthusiast
Posts: 403
Joined: Fri Apr 20, 2012 8:09 pm
Contact:

Re: Help regexp

Post 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.
google Translate;Makes my jokes fall flat- Fait mes blagues tombent à plat- Machte meine Witze verpuffen- Eh cumpari ci vo sunari
loulou2522
Enthusiast
Enthusiast
Posts: 488
Joined: Tue Oct 14, 2014 12:09 pm

Re: Help regexp

Post by loulou2522 »

In this case
txt$= "FL 100 500 100 500"
var1 must contain 100500
and var2 must contain 10500
AZJIO
Addict
Addict
Posts: 1257
Joined: Sun May 14, 2017 1:48 am

Re: Help regexp

Post 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
infratec
Always Here
Always Here
Posts: 6786
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Help regexp

Post 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.
AZJIO
Addict
Addict
Posts: 1257
Joined: Sun May 14, 2017 1:48 am

Re: Help regexp

Post 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
Marc56us
Addict
Addict
Posts: 1477
Joined: Sat Feb 08, 2014 3:26 pm

Re: Help regexp

Post 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.
Post Reply