Sign in to follow this  
Followers 0
oshaker

Match string help

4 posts in this topic

#1 ·  Posted (edited)

I have strings like this:

1. abcd xx-xx-x

2. abcd xx-xx-xx

3. abcd xx-xx-xxx

Text I want to remove is in red, the rest of the string i.e. "abcd" is not fixed. I need to make them like this:

abcd xx-xx

What is the pattern I can use to achieve this? I am using StringRegExpReplace function

Edited by oshaker

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

Hi,

although i'm quite sure that a stringregexp guru will jump the train as well:

$string1 = "abcd xx-xx-x"
$string2 = "abcd xx-xx-xx"
$string3 = "abcd xx-xx-xxx"

For $x = 1 To 3
    $par = Eval ("string" & $x)
    MsgBox (0,Eval ("string" & $x), _mysplit ($par))
Next

Func _mysplit ($string)
    $retstring = ""
    $temp = StringSplit ($string, "-", 2)
    For $i = 0 To UBound ($temp) - 2
        If $i < UBound ($temp) - 2 Then 
            $retstring &= $temp [$i] & "-"
        Else
            $retstring &= $temp [$i]
        EndIf
    Next
    Return $retstring
EndFunc

;-))

Stefan

Edited by 99ojo

Share this post


Link to post
Share on other sites

This is better: ([0-9]+\-) with count = 1 using StringRegExpReplace function :mellow:

Share this post


Link to post
Share on other sites

If the pattern is always the same, you can do it this way.

local $string[4]
local $yourstring
 $string[1] = "abcffd xx-xx-x"
 $string[2] = "abaacd xx-xx-xx"
 $string[3] = "aebcd xx-xx-xxx"

for $i = 1 to 3
$yourstring = stringtrimright($string[$i], (stringlen($string[$i]) - stringinstr($string[$i], "-",0,2)) + 1)
msgbox(1,"",$yourstring)
Next

But this construction requires the string always to be :

XXXXXXorwhateverherestandsrofl XX-XX-

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0