Jump to content

String help please


Go to solution Solved by Melba23,

Recommended Posts

I have $txt string that is changing " July 20 - August 16"
How i can get only part "August 16" I cant use  StringInStr($txt,"-") Cos $txt returns values like those " July 2 - 4" as well
Some samples of $
txt output:

July 3
July 7
July 5 - 9 *
July 2 - 4
July 20 - August 16
July 5 - 9
July 4 - 6
July 6 - 12
July 5 - 8
July 6 - 14
July 20 - September 16**
July 6 - 12

And i need to get Latest month and i need latest day, and i need to but them together.   Example September 16 ** and July 9 *
I know how to use StringLeft, mid, right etc, I juyst dont understand how to get Latest month out of string. Latest number i can just do StringRight($txt,2)
 
 

Link to comment
Share on other sites

  • Moderators
  • Solution

dersiniar,

This seems to work:

#include <Array.au3>
#include <StringConstants.au3>

$sString = "July 3" & @CRLF & _
    "July 7" & @CRLF & _
    "July 5 - 9 *" & @CRLF & _
    "July 2 - 4" & @CRLF & _
    "July 20 - August 16" & @CRLF & _
    "July 5 - 9" & @CRLF & _
    "July 4 - 6" & @CRLF & _
    "July 6 - 12" & @CRLF & _
    "July 5 - 8" & @CRLF & _
    "July 6 - 14" & @CRLF & _
    "July 20 - September 16**" & @CRLF & _
    "July 6 - 12"

$aArray = StringSplit($sString, @CRLF, $STR_ENTIRESPLIT)

;_ArrayDisplay($aArray, "", Default, 8)

For $i = 1 To $aArray[0]
    ; Look for the sequence "- Any capital letter"
    If StringRegExp($aArray[$i], "\-\s[A-Z]") Then
        ; If found then remove everythign before the capital letter
        $aArray[$i] = StringRegExpReplace($aArray[$i], "^.*\-\s(.*)$", "$1")
    EndIf
Next

_ArrayDisplay($aArray, "", Default, 8)

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

  • Developers

This is the one I came up with:

Global $test = ["July 5 - 9", "July 2 - 4", "July 20 - August 16"]
For $x = 0 To UBound($test) - 1
    $test[$x] = StringRegExpReplace($test[$x], "[^\-]*\-\s*([A-Z]+)", "$1")
Next
_ArrayDisplay($test)

 

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

This worked for me.

#include <Array.au3>
Local $sString = "July 3" & @CRLF & _
    "July 7" & @CRLF & _
    "July 5 - 9 *" & @CRLF & _
    "July 2 - 4" & @CRLF & _
    "July 20 - August 16" & @CRLF & _
    "July 5 - 9" & @CRLF & _
    "July 4 - 6" & @CRLF & _
    "July 6 - 12" & @CRLF & _
    "July 5 - 8" & @CRLF & _
    "July 6 - 14" & @CRLF & _
    "July 20 - September 16**" & @CRLF & _
    "July 6 - 12"
Local $aString = StringRegExp($sString, "(?:.*-\s)([A-Za-z]*\s[0-9]*)", 3)
_ArrayDisplay($aString)

 

Link to comment
Share on other sites

18 minutes ago, Jos said:

This is the one I came up with:

Global $test = ["July 5 - 9", "July 2 - 4", "July 20 - August 16"]
For $x = 0 To UBound($test) - 1
    $test[$x] = StringRegExpReplace($test[$x], "[^\-]*\-\s*([A-Z]+)", "$1")
Next
_ArrayDisplay($test)

 

I like your code, looks like it works. But can you explain little how it picks up this and how i can use it for variable?
If i use MsgBox(0,"",$test) i get 0 output

Link to comment
Share on other sites

  • Developers
3 minutes ago, dersiniar said:

$test

This is an Array not a regular variable, so requires [] at the end.
Obviously you can use a regular variable as well with the StringRegexReplace() line.

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

42 minutes ago, Melba23 said:

dersiniar,

This seems to work:

#include <Array.au3>
#include <StringConstants.au3>

$sString = "July 3" & @CRLF & _
    "July 7" & @CRLF & _
    "July 5 - 9 *" & @CRLF & _
    "July 2 - 4" & @CRLF & _
    "July 20 - August 16" & @CRLF & _
    "July 5 - 9" & @CRLF & _
    "July 4 - 6" & @CRLF & _
    "July 6 - 12" & @CRLF & _
    "July 5 - 8" & @CRLF & _
    "July 6 - 14" & @CRLF & _
    "July 20 - September 16**" & @CRLF & _
    "July 6 - 12"

$aArray = StringSplit($sString, @CRLF, $STR_ENTIRESPLIT)

;_ArrayDisplay($aArray, "", Default, 8)

For $i = 1 To $aArray[0]
    ; Look for the sequence "- Any capital letter"
    If StringRegExp($aArray[$i], "\-\s[A-Z]") Then
        ; If found then remove everythign before the capital letter
        $aArray[$i] = StringRegExpReplace($aArray[$i], "^.*\-\s(.*)$", "$1")
    EndIf
Next

_ArrayDisplay($aArray, "", Default, 8)

M23

Thank you, i used your code, and i got it work. ❤️ Thanks

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