RegEx 2 variables in same line
Moderators: JRL, Dorian (MJT support)
RegEx 2 variables in same line
Hi.
i want to scan a 10 lines file to find 2 variables in the same line.
GBP and minus(-)
Is it possible with regex?
i want to scan a 10 lines file to find 2 variables in the same line.
GBP and minus(-)
Is it possible with regex?
Re: RegEx 2 variables in same line
Hi, Can you give an example.
Re: RegEx 2 variables in same line
Yes. the line is this
GBP_NZD 200 B -8 -108.26
so i want to know if both my variables (GBP and -) are in the same line
GBP_NZD 200 B -8 -108.26
so i want to know if both my variables (GBP and -) are in the same line
Re: RegEx 2 variables in same line
I see you have two -. Are both relevant or anyone of them? Do you just want to check if info is there or do you want to extract anything?
Re: RegEx 2 variables in same line
Any of them..
I just want to check if both GBP and - exists but only in the same line.
the text to scan is this
GBP_NZD 213 S -14.6 -310.98
AUD_JPY 500 S -13.7 -626.07
GBP_CAD 1 S 10.4 0.79
USD_JPY 1 S -2.3 -0.21
GBP_JPY 343 S 10 313.49
CAD_JPY 500 S 11.8 539.24
GBP_JPY 500 S 29.4 1,343.53
I just want to check if both GBP and - exists but only in the same line.
the text to scan is this
GBP_NZD 213 S -14.6 -310.98
AUD_JPY 500 S -13.7 -626.07
GBP_CAD 1 S 10.4 0.79
USD_JPY 1 S -2.3 -0.21
GBP_JPY 343 S 10 313.49
CAD_JPY 500 S 11.8 539.24
GBP_JPY 500 S 29.4 1,343.53
Re: RegEx 2 variables in same line
Hi, maybe like this:
Code: Select all
Let>string=GBP_NZD 200 B -8 -108.26
Let>tmp0=(?m-s)^.*GBP.*-.*-
RegEx>tmp0,string,0,m,nm,0
If>nm>0
MDL>Yes, they are there
EndIf
Re: RegEx 2 variables in same line
If you just care for one - (either of them then) then you can just change tmp0 to (now it looks for both):
Code: Select all
(?m-s)^.*GBP.*-
Re: RegEx 2 variables in same line
Great!!!
Thank you very much. I have to read more on regex..
looks very usefull
Thank you very much. I have to read more on regex..
looks very usefull

Re: RegEx 2 variables in same line
It's very powerful and worth learning (at least the basics). If you had also needed the relevant lines then that can also easily be extracted, example below for your future study...:
Code: Select all
LabelToVar>Test,string
Let>tmp0=(?m-s)^.*GBP.*-.+
RegEx>tmp0,string,0,m,nm,0
Let>res=
If>nm>0
Let>ctr=0
While>ctr<nm
Add>ctr,1
Let>tmp=m_%ctr%
Let>res=%res%%tmp%
EndWhile
MDL>Yes, they are there:%CRLF%%res%
EndIf
/*
Test:
GBP_NZD 213 S -14.6 -310.98
AUD_JPY 500 S -13.7 -626.07
GBP_CAD 1 S 10.4 0.79
USD_JPY 1 S -2.3 -0.21
GBP_JPY 343 S 10 313.49
CAD_JPY 500 S 11.8 539.24
GBP_JPY 500 S 29.4 1,343.53
GBP_NZD 213 S -14.6 310.98
GBP_NZD 213 S 14.6 -310.98
Re: RegEx 2 variables in same line
Hi hagchr
I have one more question if you can help me.
Is it possible to know in which line in the text found the results?
like ( line1,line2 etc)
Thank you very much for your help
I have one more question if you can help me.
Is it possible to know in which line in the text found the results?
like ( line1,line2 etc)
Thank you very much for your help
Re: RegEx 2 variables in same line
Hi, You need to add a couple of lines to get the row numbers. As always there are different ways, but if we continue with RegEx, what you can do is to, for each match, do a new search from the beginning of the (full) string until the end of the match and simply count the number of end of lines/string.
You could get a problem if you have duplicate matches later on in the data (you would find the row number of the first match instead of the later one). To avoid that, when each match is completed it is replaced with xxxx (in the temp string used in the loop). Hope it helps, come back if you have questions.
You could get a problem if you have duplicate matches later on in the data (you would find the row number of the first match instead of the later one). To avoid that, when each match is completed it is replaced with xxxx (in the temp string used in the loop). Hope it helps, come back if you have questions.
Code: Select all
LabelToVar>Test,string
Let>tmp0=(?m-s)^.*GBP.*-.+
RegEx>tmp0,string,0,m,nm,0
//Loop over each of the nm matches and find the row number
Let>tmpstring=string
Let>ctr=0
While>ctr<nm
Add>ctr,1
//Find a substring - beginning of full string until end of current match -> m1_1
Let>tmp1=m_%ctr%
Let>tmp0=(?ms)\A.*?%tmp1%
RegEx>tmp0,tmpstring,0,m1,nm1,0
//Look for end of line/string in string m1_1 - number of matches = row number -> nm2
RegEx>(?m)($|\Z),m1_1,0,m2,nm2,0
//Replace matched record with xxxx -> tmp2
RegEx>tmp1,m1_1,0,m3,nm3,1,xxxx,tmp2
//Replace the string (Beginning to end of match) with the version with xxxx in it
Let>tmp0=(?ms)\A.*?%tmp1%
RegEx>tmp0,tmpstring,0,m4,nm4,1,tmp2,tmpstring
//Assign row number -> tmpRow_%ctr%
Let>tmpRow_%ctr%=nm2
EndWhile
//Prepare/present result
Let>res=
If>nm>0
Let>ctr=0
While>ctr<nm
Add>ctr,1
Let>tmp=m_%ctr%
Let>tmp1=tmprow_%ctr%
Let>res=%res%%tmp1%: %tmp%%CRLF%
EndWhile
MDL>Yes, they are there:%CRLF%%res%
Else
MDL>No matches
EndIf
/*
Test:
GBP_NZD 213 S -14.6 -310.98
AUD_JPY 500 S -13.7 -626.07
GBP_CAD 1 S 10.4 0.79
USD_JPY 1 S -2.3 -0.21
GBP_JPY 343 S 10 313.49
CAD_JPY 500 S 11.8 539.24
GBP_JPY 500 S 29.4 1,343.53
GBP_NZD 213 S -14.6 310.98
GBP_NZD 213 S 14.6 -310.98
*/
Re: RegEx 2 variables in same line
Hi again, originally you asked for RexEx, so I replied in RegEx. However, you could also solve the whole thing with a simple loop. If the data set is small then there is no performance difference and probably easier to follow for anyone not into RegEx.
Code: Select all
LabelToVar>Test,string
Separate>string,CRLF,arrString
Let>searchstring1=GBP
Let>searchstring2=-
Let>matchctr=0
Let>ctr=0
While>ctr<arrString_COUNT
Add>ctr,1
Let>tmp=arrString_%ctr%
Position>searchstring1,tmp,1,res1
If>{%res1%<>0}
Position>searchstring2,tmp,1,res2
If>{%res2%<>0}
Add>matchctr,1
Let>row_%matchctr%=ctr
Let>match_%matchctr%=tmp
Endif
EndIf
EndWhile
//Prepare/present result
Let>res=
If>matchctr>0
Let>ctr=0
While>ctr<matchctr
Add>ctr,1
Let>tmp=match_%ctr%
Let>tmp1=row_%ctr%
Let>res=%res%%tmp1%: %tmp%%CRLF%
EndWhile
MDL>Yes, they are there:%CRLF%%res%
Else
MDL>No matches
EndIf
/*
Test:
GBP_NZD 213 S -14.6 -310.98
AUD_JPY 500 S -13.7 -626.07
GBP_CAD 1 S 10.4 0.79
USD_JPY 1 S -2.3 -0.21
GBP_JPY 343 S 10 313.49
CAD_JPY 500 S 11.8 539.24
GBP_JPY 500 S 29.4 1,343.53
GBP_NZD 213 S -14.6 310.98
GBP_NZD 213 S 14.6 -310.98
Re: RegEx 2 variables in same line
Thank you hagchr for sharing your experience.!!!