OT: RegEx

OT: RegEx

Postby hua » Mon Mar 08, 2010 8:18 am

Do we have any regex guru here?

String in question,
Code: Select all  Expand view

"2601-2650 21 - - - - - - - - - - -21 15 - - - - - - - - - "
 


I want to capture the 2nd occurrence of "-2". I came up with "[^\d]-\d" but it'll return " -2" (preceded by a space) not "-2"

TIA
Last edited by hua on Mon Mar 08, 2010 3:32 pm, edited 1 time in total.
FWH 11.08/FWH 19.12
BCC5.82/BCC7.3
xHarbour/Harbour
hua
 
Posts: 1072
Joined: Fri Oct 28, 2005 2:27 am

Re: OT: RegEx

Postby nageswaragunupudi » Mon Mar 08, 2010 12:19 pm

I do not know anything about RegEx.
But if the purpose is limited to finding second ( or nth ) occurrence of a string in another string, we have enough built-in functions in (x)Harbour.
Code: Select all  Expand view
  c  := "2601-2650 21 - - - - - - - - - - -21 15 - - - - - - - - - "
   n  := AtNum( "-2", c, 2 ) // find '-2' in c 2nd occurrence
   ? n    // 34
   ? SubStr( c, n  )  // "-21 15 --    "
 

We need to link ct.lib of xHarbour or hbct.lib of Harbour
Regards

G. N. Rao.
Hyderabad, India
User avatar
nageswaragunupudi
 
Posts: 10627
Joined: Sun Nov 19, 2006 5:22 am
Location: India

Re: OT: RegEx

Postby lailton.webmaster » Mon Mar 08, 2010 2:35 pm

"-([0-9]{1,})"
lailton.webmaster
 
Posts: 603
Joined: Sun May 04, 2008 8:44 pm

Re: OT: RegEx

Postby hua » Mon Mar 08, 2010 3:32 pm

@Rao,
Thanks for the tip. I didn't know about AtNum() but regular expression kinda fascinates me now. I'm parsing a tax table and I find regular expression allows me to write neat and tidy code and regex power is awesome. Though it's kinda dizzying as it's another new thing to learn :)

@Lailton,
I just noticed I made a typo. A thousand apologies, the result that I'm after is "-2" not "21". Sorry for the confusion to anyone who's reading this thread. But your expression seems better than mine I'll test it out. Thanks.
FWH 11.08/FWH 19.12
BCC5.82/BCC7.3
xHarbour/Harbour
hua
 
Posts: 1072
Joined: Fri Oct 28, 2005 2:27 am

Re: OT: RegEx

Postby lailton.webmaster » Mon Mar 08, 2010 5:49 pm

what´s the result that u wanna

"2601-2650 21 - - - - - - - - - - -21 15 - - - - - - - - - "

is ?
-26
-21
lailton.webmaster
 
Posts: 603
Joined: Sun May 04, 2008 8:44 pm

Re: OT: RegEx

Postby lailton.webmaster » Mon Mar 08, 2010 6:02 pm

function main

local i, aMatch
local er :="([-][2][0-9])"
local cString:="2601-2650 21 - - - - - - - - - - -21 15 - - - - - - - - - "

aMatch := HB_RegExAll( er, cString, .f., .t. )

if (aMatch != Nil)

for i = 1 to len(aMatch)
alert(aMatch[i][1])
next i

else

alert("Not found !")

endif

return


I dont know if a understand what u wanna more try this, the return is -26 and -21.
lailton.webmaster
 
Posts: 603
Joined: Sun May 04, 2008 8:44 pm

Re: OT: RegEx

Postby hua » Tue Mar 09, 2010 2:04 am

lailton.webmaster wrote:what´s the result that u wanna

"2601-2650 21 - - - - - - - - - - -21 15 - - - - - - - - - "

is ?
-26
-21


Hi Lailton,
I'm after the 2nd occurrence of "a hyphen followed by a digit". To make it clearer, let me expand the data to process
Code: Select all  Expand view

2601-2650 21 - - - - - - - - - - -21 15 - - - - - - - - -
3801-3850 146 54 48 42 37 31 25 19 13 - - -146 136 93 87 82 76 70 64 58 52 47
3851-3900 152 58 52 46 40 34 28 23 17 11 - -152 142 132 91 85 79 73 68 62 56 50
3901-3950 158 128 55 49 44 38 32 26 20 14 - -158 148 138 128 89 83 77 71 65 59 54
3951-4000 164 134 59 53 47 41 35 30 24 18 12 -164 154 144 134 92 86 80 75 69 63 57
4001-4050 170 140 130 56 51 45 39 33 27 21 16 10 170 160 150 140 130 90 84 78 72 66 61
 


The result that I'm hoping to see for each line above respectively would be;

-2
-1
-1
-1
-1
< none >

Thank you.
FWH 11.08/FWH 19.12
BCC5.82/BCC7.3
xHarbour/Harbour
hua
 
Posts: 1072
Joined: Fri Oct 28, 2005 2:27 am

Re: OT: RegEx

Postby Antonio Linares » Tue Mar 09, 2010 3:54 am

Hua,

local er :="([-][0-9])"

that may work
regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
Antonio Linares
Site Admin
 
Posts: 42082
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain

Re: OT: RegEx

Postby hua » Tue Mar 09, 2010 5:01 am

Hey, you're very close Antonio :). Now just need to tweak it further to make it ignore hyphen and digit in column 5 & 6.
Thank you.
FWH 11.08/FWH 19.12
BCC5.82/BCC7.3
xHarbour/Harbour
hua
 
Posts: 1072
Joined: Fri Oct 28, 2005 2:27 am

Re: OT: RegEx

Postby Marcelo Via Giglio » Tue Mar 09, 2010 3:59 pm

Hi,

try with

local er :="(\ [-][0-9])"

regards

Marcelo
Marcelo Via Giglio
 
Posts: 1064
Joined: Fri Oct 07, 2005 3:33 pm
Location: Cochabamba - Bolivia

Re: OT: RegEx

Postby hua » Fri Mar 12, 2010 1:24 am

Thanks Marcelo. But that didn't do it
FWH 11.08/FWH 19.12
BCC5.82/BCC7.3
xHarbour/Harbour
hua
 
Posts: 1072
Joined: Fri Oct 28, 2005 2:27 am

Re: OT: RegEx

Postby Marcelo Via Giglio » Fri Mar 12, 2010 1:47 am

Hi,

it is starnge, because in my test it work, I put the string "2601-2650 21 - - - - - - - - - - -21 15 - - - - - - - - - " and the result was only -2
mybe "( +[-][0-9])" (space+[-][0-9])




hua wrote:Thanks Marcelo. But that didn't do it
Marcelo Via Giglio
 
Posts: 1064
Joined: Fri Oct 07, 2005 3:33 pm
Location: Cochabamba - Bolivia

Re: OT: RegEx

Postby lailton.webmaster » Fri Mar 12, 2010 3:20 pm

([ ]?[-][0-9])
lailton.webmaster
 
Posts: 603
Joined: Sun May 04, 2008 8:44 pm


Return to FiveWin for Harbour/xHarbour

Who is online

Users browsing this forum: No registered users and 94 guests