Jump to content

StringReplace question


 Share

Recommended Posts

I need help with StringReplace to remove field titles so that

George Mundine Client Name 2:

Marital Status: RSD Address 1: BLK 40 Sydney St.

RSD Address 2: #02-113 RSD Address 3:

PostCode: 210040

becomes:

George Mundine

BLK 40 Sydney St. #02-113 RSD

Washington 210040

Input fields have variable length like, in this case, "Client Name 2:" and "Marital Status:" are blank so I need to delete from "Client Name 2:" to "RSD Address 1:" etc.

Link to comment
Share on other sites

You're looking for StringRegExp in the manual... And this tool will help.

I'm trying to learn from what you wrote there, as i'm trying to learn more and more about Regular Expressions, but i'm a bit perplexed.

$string = 'Client Name 1: George Mundine Client Name 2: Junior'
$string = $string & 'Marital Status: Single RSD Address 1: BLK 40 Sydney St.'
$string = $string & 'RSD Address 2: #02-113 RSD Address 3: Upper Loft'
$string = $string & 'PostCode: 210040'

$nOffset = 1
While 1
    $array = StringRegExp($string, "(?s)(?i)(?:Client Name 1):(.{1,50})(?:Client Name 2:)(.*?)(?:Marital status:)(.*?)(?:RSD Address 1:)(.*?)(?:RSD Address 2:)(.*?)(?:RSD Address 3:)(.*?)(?:PostCode:)(.*?)(?:\z)", 1, $nOffset)
    
    If @error = 0 Then
        $nOffset = @extended
    Else
        ExitLoop
    EndIf
    
        $name = $array[0] & ", " & $array[1]
        $status = $array[2]
        $address1 = $array[3] & " " & $array[4]
        $address2 = $array[5]
        $zip = $array[6]
WEnd

MsgBox(0, "test", $name)
MsgBox(0, "test", $status)
MsgBox(0, "test", $address1)
MsgBox(0, "test", $address2)
MsgBox(0, "test", $zip)

Note: i did add 'Client Name 1:' in there only because i figured i'd follow along with the pattern. He may not have it, but i can be removed w/ the same results, so it's just there for testing.

Is there not a simpler way to accomplish this? Granted, i just GUESSED 100% at what it should be, and i get all of the accurate results there, but i'm just confused as to how you came up with your example. One would think that he could just use StringReplace to remove the titles and just parse the string for what he wants; but i'm intrigued by what you did there with regex's

Tho, is there not a simpler way, that huge regex there seems........odd to me.

Edited by zackrspv

-_-------__--_-_-____---_-_--_-__-__-_ ^^€ñ†®øÞÿ ë×阮§ wï†høµ† ƒë@®, wï†høµ† †ïmë, @ñd wï†høµ† @ †ïmïdï†ÿ ƒø® !ïƒë. €×阮 ñø†, bµ† ïñ§†ë@d wï†hïñ, ñ@ÿ, †h®øµghøµ† †hë 맧ëñ§ë øƒ !ïƒë.

Link to comment
Share on other sites

Another approach...

$Array[1] = "George Mundine Client Name 2:"
$Array[2] = "Marital Status: RSD Address 1: BLK 40 Sydney St."
$Array[3] = "RSD Address 2: #02-113 RSD Address 3:"
$Array[4] = "PostCode: 210040"

    
    $Array[1] = StringLeft($Array[1], StringInStr($Array[1], "Client"))
    
    $Array[3] = StringLeft($Array[3], StringInStr($Array[3], " Address 3:"))
    
    $Array[3] = StringTrimLeft($Array[3], StringInStr($Array[3], "2:") + 1)
    
    $Array[2] = StringTrimLeft($Array[2], StringInStr($Array[2], "1:")+ 2) & $Array[3]
    
    $Array[3] =  ZipCode( StringtrimLeft($Array[4], StringInStr($Array[4], ":")))
    
    _ArrayDelete($Array, 4)
    _ArrayDisplay($Array)    
    
    
Func ZipCode($Info)
    
    If StringInStr($Info, "210040") Then Return "Washington" & $Info
    
    ; add zip codes as desired
    
EndFunc

8)

NEWHeader1.png

Link to comment
Share on other sites

I'm trying to learn from what you wrote there, as i'm trying to learn more and more about Regular Expressions, but i'm a bit perplexed.

$string = 'Client Name 1: George Mundine Client Name 2: Junior'
$string = $string & 'Marital Status: Single RSD Address 1: BLK 40 Sydney St.'
$string = $string & 'RSD Address 2: #02-113 RSD Address 3: Upper Loft'
$string = $string & 'PostCode: 210040'

$nOffset = 1
While 1
    $array = StringRegExp($string, "(?s)(?i)(?:Client Name 1):(.{1,50})(?:Client Name 2:)(.*?)(?:Marital status:)(.*?)(?:RSD Address 1:)(.*?)(?:RSD Address 2:)(.*?)(?:RSD Address 3:)(.*?)(?:PostCode:)(.*?)(?:\z)", 1, $nOffset)
    
    If @error = 0 Then
        $nOffset = @extended
    Else
        ExitLoop
    EndIf
    
        $name = $array[0] & ", " & $array[1]
        $status = $array[2]
        $address1 = $array[3] & " " & $array[4]
        $address2 = $array[5]
        $zip = $array[6]
WEnd

MsgBox(0, "test", $name)
MsgBox(0, "test", $status)
MsgBox(0, "test", $address1)
MsgBox(0, "test", $address2)
MsgBox(0, "test", $zip)

Note: i did add 'Client Name 1:' in there only because i figured i'd follow along with the pattern. He may not have it, but i can be removed w/ the same results, so it's just there for testing.

Is there not a simpler way to accomplish this? Granted, i just GUESSED 100% at what it should be, and i get all of the accurate results there, but i'm just confused as to how you came up with your example. One would think that he could just use StringReplace to remove the titles and just parse the string for what he wants; but i'm intrigued by what you did there with regex's

Tho, is there not a simpler way, that huge regex there seems........odd to me.

He could definitely do that, and very likely get the same results, it's just not going to be as elegant as using Regular Expressions... They're really good for doing string matching where you know that the string is going to follow a pattern but you don't know exactly what the string is going to be.

Here's a way (with StringRegExp you can do the same thing about 4 different ways) to get the info from the first line with StringRegExp + a little explanation of the verbiage:

$string = ''
$string &= 'Client Name 1: George Mundine Client Name 2: Junior' & @CRLF
$string &= 'Marital Status: Single RSD Address 1: BLK 40 Sydney St.' & @CRLF
$string &= 'RSD Address 2: #02-113 RSD Address 3: Upper Loft' & @CRLF
$string &= 'PostCode: 210040' & @CRLF

$ClientName1 = StringRegExp ($string, "(?:Client Name 1: )(.{1,50})(?:Client Name 2:)", 1)
#cs
                                       (?:text) means match but exclude the word text
                                       . means match anything
                                       {x,y} means match at least x times, up to y times
                                       If you're using the excluding (:?text) then you have to put each
                                        set in ()
#ce                                 
$ClientName2 = StringRegExp ($string, "(?:Client Name 1: )(?:.{1,50})(?:Client Name 2:)(.{1,50})", 1)
MsgBox (0, "", "Client Name 1: " & $ClientName1[0] & @CRLF & "Client Name 2:" & $ClientName2[0] & @CRLF)

I went ahead and added a @CRLF to each line to make it an actual line as far as $string was concerned for easier matching, but as far as his original question is concerned, if he just gets each of those lines as an individual string it'd be a lot easier to match one line at a time rather than trying to match out of the whole thing. You could still do it, it'd just end up being 4 shorter RegExp codes rather than one really big one.

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...