# RegeX Help

## Recommended Posts

The last part of my string is in any of the below formats :
productCode-quantity pairs and discount

Max number of pairs will be 5 always separated by hyphen
In some cases , less than 5 pairs , followed by either - or <blank>. Number of loose hyphens or spaces is not always consistent.
Always followed by a unpaired number

\$input = 2-3 3-2 4-1 5-2 5
\$input = 2-3 3-2 4-1 5-2 - 6
\$input = 2-3 3-2 4-1 - - 6
\$input = 2-3 3-2 4-1  6
\$input = 7-3 9-4 6-4 5-4 3-4 5
\$input = 4-4 6-3 3-3 2-2 - 4
\$input = 14-3 16-2 13-1 12-3 10-1 4
\$input = 14-3 16-2 13-1 12-3 10-1 12
\$input = 2-3 3-2 4-1 -  6

i want to obtain result such
\$productCode[1] = first digit of pair
\$productCode[2] = first digit of pair
\$productCode[5] = first digit of pair

\$quantity[1] = second digit of pair
\$quantity[2] = second digit of pair
\$quantity[5] = second digit of pair

\$discount = last number

In case , less than 5
make \$productCode[4] = "-"

Trying to use ,

Struggling with regex.

To border it with | , then shall use sting split to seperate the two.

Question  2 :

This does not work for April , how to manage the \A.

Question  3 :

Say I wish to find any email address , and if found surround it with | on both side.
ALso , Can it be done to limit the search from and to certain point in the entire line and from right to left.

##### Share on other sites

?

```#Include <Array.au3>

;\$s = "2-3 3-2 4-1 - -   6"
\$s = "14-3 16-2 13-1 12-3 10-1 12"

\$a = StringRegExp(\$s, '(?|(\d+)-(\d+)|\d+(?=\$))', 3)
;_ArrayDisplay(\$a)

\$n = (UBound(\$a)-1)/2  ; nb of pairs
Local \$res[\$n][3]
For \$i = 0 to \$n-1
\$res[\$i][0] = \$a[\$i*2]   ; col 0 = productCode
\$res[\$i][1] = \$a[\$i*2+1] ; col 1 = quantity
Next
\$res[0][2] = \$a[\$n*2]        ; col 2 = discount

_ArrayDisplay(\$res)```

• 1

##### Share on other sites
20 hours ago, mikell said:

?

```#Include <Array.au3>

;\$s = "2-3 3-2 4-1 - -   6"
\$s = "14-3 16-2 13-1 12-3 10-1 12"

\$a = StringRegExp(\$s, '(?|(\d+)-(\d+)|\d+(?=\$))', 3)
;_ArrayDisplay(\$a)

\$n = (UBound(\$a)-1)/2  ; nb of pairs
Local \$res[\$n][3]
For \$i = 0 to \$n-1
\$res[\$i][0] = \$a[\$i*2]   ; col 0 = productCode
\$res[\$i][1] = \$a[\$i*2+1] ; col 1 = quantity
Next
\$res[0][2] = \$a[\$n*2]        ; col 2 = discount

_ArrayDisplay(\$res)```

Wow , Thanks. Shall try and let you know.

##### Share on other sites

It is working very well.
Can you help me put it in this format :
|3-2|2-3|14-5|6-4|99-99|99-99|5
deleting/replacing the existing occurrence is seeming difficult.
Trimming out the dangling hyphens and spaces left and right.

UMRD LOS BANOS CA 6036195773 2-3 4-4 1-3 --5
will become :
UMRD LOS BANOS CA 6036195773|2-3|4-4|1-3|99-99|99-99|5

The last digit being the discount.
This will come at the end of my line.

I changed the array size , as there always have to be 5 , (Local \$res[5][3]) ,  records. In case original data has lesser , fill up the rest with 99.

```\$a = StringRegExp(\$lastsegment, '(?|(\d+)-(\d+)|\d+(?=\$))', 3)
\$n = (UBound(\$a)-1)/2  ; nb of pairs
Local \$res[5][3]
If \$n > 1 Then
For \$i = 0 to \$n-1
\$res[\$i][0] = \$a[\$i*2]   ; col 0 = productCode
\$res[\$i][1] = \$a[\$i*2+1] ; col 1 = quantity
Next
\$res[0][2] = \$a[\$n*2]        ; col 2 = discount
For \$j=\$n to 4 Step 1
\$res[\$j][0] = "99"   ; col 0 = productCode
\$res[\$j][1] = "99" ; col 1 = quantity
Next
EndIf```

##### Share on other sites

??

```\$string = "UMRD LOS BANOS CA 6036195773 2-3 4-4 1-3 --5"

\$s = StringRegExpReplace(\$string, '.*?(\h\d+-\d+[\h\d-]+\d\$)', "\$1")
;Msgbox(0,"", \$s)
\$b = StringRegExp(\$s, '(\d+-\d+)|\d+(?=\$)', 3)
;_ArrayDisplay(\$b)
Local \$n = UBound(\$b)-1, \$res = ""
For \$i = 0 to 4
\$res &= "|" & (\$i<\$n ? \$b[\$i] : "99-99")
Next
\$res &= "|" & \$b[\$n]
;Msgbox(0,"", \$res)

Msgbox(0,"", StringReplace(\$string, \$s, \$res) )```

## Create an account

Register a new account

×

• Wiki

• Back

• Git