RegEx 2 variables in same line

Technical support and scripting issues

Moderators: Dorian (MJT support), JRL

Post Reply
conjure
Pro Scripter
Posts: 63
Joined: Thu Jan 12, 2012 3:05 pm

RegEx 2 variables in same line

Post by conjure » Mon May 23, 2016 2:11 pm

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?

hagchr
Automation Wizard
Posts: 327
Joined: Mon Jul 05, 2010 7:53 am
Location: Stockholm, Sweden

Re: RegEx 2 variables in same line

Post by hagchr » Mon May 23, 2016 2:14 pm

Hi, Can you give an example.

conjure
Pro Scripter
Posts: 63
Joined: Thu Jan 12, 2012 3:05 pm

Re: RegEx 2 variables in same line

Post by conjure » Mon May 23, 2016 2:16 pm

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

hagchr
Automation Wizard
Posts: 327
Joined: Mon Jul 05, 2010 7:53 am
Location: Stockholm, Sweden

Re: RegEx 2 variables in same line

Post by hagchr » Mon May 23, 2016 2:22 pm

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?

conjure
Pro Scripter
Posts: 63
Joined: Thu Jan 12, 2012 3:05 pm

Re: RegEx 2 variables in same line

Post by conjure » Mon May 23, 2016 2:23 pm

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

hagchr
Automation Wizard
Posts: 327
Joined: Mon Jul 05, 2010 7:53 am
Location: Stockholm, Sweden

Re: RegEx 2 variables in same line

Post by hagchr » Mon May 23, 2016 2:35 pm

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

hagchr
Automation Wizard
Posts: 327
Joined: Mon Jul 05, 2010 7:53 am
Location: Stockholm, Sweden

Re: RegEx 2 variables in same line

Post by hagchr » Mon May 23, 2016 2:40 pm

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.*-

conjure
Pro Scripter
Posts: 63
Joined: Thu Jan 12, 2012 3:05 pm

Re: RegEx 2 variables in same line

Post by conjure » Mon May 23, 2016 2:43 pm

Great!!!
Thank you very much. I have to read more on regex..
looks very usefull :)

hagchr
Automation Wizard
Posts: 327
Joined: Mon Jul 05, 2010 7:53 am
Location: Stockholm, Sweden

Re: RegEx 2 variables in same line

Post by hagchr » Mon May 23, 2016 2:53 pm

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

conjure
Pro Scripter
Posts: 63
Joined: Thu Jan 12, 2012 3:05 pm

Re: RegEx 2 variables in same line

Post by conjure » Sat Mar 25, 2017 10:39 am

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

hagchr
Automation Wizard
Posts: 327
Joined: Mon Jul 05, 2010 7:53 am
Location: Stockholm, Sweden

Re: RegEx 2 variables in same line

Post by hagchr » Sun Mar 26, 2017 8:09 am

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.

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
*/

hagchr
Automation Wizard
Posts: 327
Joined: Mon Jul 05, 2010 7:53 am
Location: Stockholm, Sweden

Re: RegEx 2 variables in same line

Post by hagchr » Sun Mar 26, 2017 9:16 am

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

conjure
Pro Scripter
Posts: 63
Joined: Thu Jan 12, 2012 3:05 pm

Re: RegEx 2 variables in same line

Post by conjure » Sun Mar 26, 2017 7:48 pm

Thank you hagchr for sharing your experience.!!!

Post Reply
Sign up to our newsletter for free automation tips, tricks & discounts