Jump to content

Moving the first word of a string to the end


Recommended Posts

  • Moderators

lostandconfused,

This seems to work:

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

$sText = "start1 blah end1" & @CRLF & _
        "start2 blah end2" & @CRLF & _
        "start3 blah end3" & @CRLF & _
        "start4 blah end4" & @CRLF & _
        "start5 blah end5" & @CRLF & _
        "start6 blah end6"

; Simulate reading the file into an array with _FileReadToArray
$aLines = StringSplit($sText, @CRLF, $STR_ENTIRESPLIT)

; Loop through the array and alter each line
For $i = 1 To $aLines[0]
    $aLines[$i] = StringRegExpReplace($aLines[$i], "(?U)^(.*)(\s.*\s)(.*)$", "$3$2$1")
Next

; Here is the result - now use _FileWriteFromAray to rewrite it
_ArrayDisplay($aLines, "", Default, 8)

RegEx decode:

(?U)      - Not greedy , look for smallest match
^         - Start of string
(.*)      - Capture any characters up to
(\s.*\s)  - the first space, which starts this captured group until the final space
(.*)      - when we capture the final characters until
$         - the end of the string

$3$2$1    - Rewrite the captured groups in the reverse order

I will see if I can now develop a single pass RegEx - although a guru will probably beat me to it!

M23

Edit: Easier than I thought:

#include <MsgBoxConstants.au3>

; Simulate reading the file into a variable with FileRead
$sText = "start1 blah end1" & @CRLF & _
        "start2 blah end2" & @CRLF & _
        "start3 blah end3" & @CRLF & _
        "start4 blah end4" & @CRLF & _
        "start5 blah end5" & @CRLF & _
        "start6 blah end6"


$sNewText = StringRegExpReplace($sText, "(?Um)^(.*)(\s.*\s)(.*)$", "$3$2$1")

; And here is the result
MsgBox($MB_SYSTEMMODAL, "New", $sNewText)

; Now rewrite the file with FileWrite

Similar RegEx decode  - but with the (?m) multiline option added to treat each line as a separate string.

Edited by Melba23

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

  • Moderators

kylomas,

And this was certainly one of them!

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

This could be done without using an array  ;)

$txt = "1 number of this line" & @CRLF & _
    "2 number of this line" & @CRLF & @CRLF & _
    "3 number of this line" & @CRLF & _
    "4 number of this line"

; $txt = FileRead("1.txt")
Msgbox(0,"", $txt)

$txt = StringRegExpReplace($txt, '(?m)^(\S+)\h+(.*)$', "$2 $1")
Msgbox(0,"", $txt)

 

Link to comment
Share on other sites

a 'regex-free' way...

Local $sString = 'one two three four five six seven eight nine ten'

ConsoleWrite(_StringSwapFirstLast($sString) & @CRLF)

Func _StringSwapFirstLast($sMyString)
    Local $iFirstSpacePos = StringInStr($sString, " ")
    Local $iLastSpacePos = StringInStr($sString, " ", 0, -1)

    Local $FirstWord = StringLeft($sString, $iFirstSpacePos - 1)
    Local $LastWord = StringRight($sString, StringLen($sString) - $iLastSpacePos)

    Return $LastWord & StringMid($sString, $iFirstSpacePos, $iLastSpacePos - $iFirstSpacePos + 1) & $FirstWord
EndFunc   ;==>_StringSwapFirstLast

 

 

image.jpeg.9f1a974c98e9f77d824b358729b089b0.jpeg Chimp

small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt....

Link to comment
Share on other sites

  • Moderators

mikell,

Quote

This could be done without using an array

Beat you for once!

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

41 minutes ago, Melba23 said:

lostandconfused,

This seems to work:

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

$sText = "start1 blah end1" & @CRLF & _
        "start2 blah end2" & @CRLF & _
        "start3 blah end3" & @CRLF & _
        "start4 blah end4" & @CRLF & _
        "start5 blah end5" & @CRLF & _
        "start6 blah end6"

; Simulate reading the file into an array with _FileReadToArray
$aLines = StringSplit($sText, @CRLF, $STR_ENTIRESPLIT)

; Loop through the array and alter each line
For $i = 1 To $aLines[0]
    $aLines[$i] = StringRegExpReplace($aLines[$i], "(?U)^(.*)(\s.*\s)(.*)$", "$3$2$1")
Next

; Here is the result - now use _FileWriteFromAray to rewrite it
_ArrayDisplay($aLines, "", Default, 8)

RegEx decode:

(?U)      - Not greedy , look for smallest match
^         - Start of string
(.*)      - Capture any characters up to
(\s.*\s)  - the first space, which starts this captured group until the final space
(.*)      - when we capture the final characters until
$         - the end of the string

$3$2$1    - Rewrite the captured groups in the reverse order

I will see if I can now develop a single pass RegEx - although a guru will probably beat me to it!

M23

Edit: Easier than I thought:

#include <MsgBoxConstants.au3>

; Simulate reading the file into a variable with FileRead
$sText = "start1 blah end1" & @CRLF & _
        "start2 blah end2" & @CRLF & _
        "start3 blah end3" & @CRLF & _
        "start4 blah end4" & @CRLF & _
        "start5 blah end5" & @CRLF & _
        "start6 blah end6"


$sNewText = StringRegExpReplace($sText, "(?Um)^(.*)(\s.*\s)(.*)$", "$3$2$1")

; And here is the result
MsgBox($MB_SYSTEMMODAL, "New", $sNewText)

; Now rewrite the file with FileWrite

Similar RegEx decode  - but with the (?m) multiline option added to treat each line as a separate string.

Thank you all. The code above does great, but it moves the first two words to the back on the line. Playing around with the settings now...

Link to comment
Share on other sites

Can yall tell me why the extra stuff in your expressions make them optimal over something like:

$sString = "start1 blah end1" & @CRLF & _
        "start2 blah end2" & @CRLF & _
        "start3 blah end3" & @CRLF & _
        "start4 blah end4" & @CRLF & _
        "start5 blah end5" & @CRLF & _
        "start6 blah end6"

msgbox(0, '' , stringregexpreplace($sString , "(.+?\b)(.*)" , "$2 $1"))

 

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Link to comment
Share on other sites

Ah, i was thinking the multiline in yours had other uses, I had not even considered formatting.

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Link to comment
Share on other sites

  • Moderators

lostandconfused,

Ooops! Try this instead:

#include <MsgBoxConstants.au3>

; Simulate reading the file into a variable with FileRead
$sText = "start1 blah fred end1" & @CRLF & _
        "start2 blah fred end2" & @CRLF & _
        "start3 blah fred end3" & @CRLF & _
        "start4 blah fred end4" & @CRLF & _
        "start5 blah fred end5" & @CRLF & _
        "start6 blah fred end6"


$sNewText = StringRegExpReplace($sText, "(?Um)^(\S*)\s(.*)\s(\S*)$", "$3 $2 $1")

; And here is the result
MsgBox($MB_SYSTEMMODAL, "New", $sNewText)

; Now rewrite the file with FileWrite

Decode:

(?Um)    - Non-greedy and multiline
^        - Start of line
(\S*)    - Caprture any non-spaces until
\s       - first space
(.*)     - Capture all characters until
\s       - last space
(\S*)    - Capture all nonspace characters until
$        - end of line

$3 $2 $1 - Reassmeble inserting spaces

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

3 minutes ago, Melba23 said:

lostandconfused,

Ooops! Try this instead:

#include <MsgBoxConstants.au3>

; Simulate reading the file into a variable with FileRead
$sText = "start1 blah fred end1" & @CRLF & _
        "start2 blah fred end2" & @CRLF & _
        "start3 blah fred end3" & @CRLF & _
        "start4 blah fred end4" & @CRLF & _
        "start5 blah fred end5" & @CRLF & _
        "start6 blah fred end6"


$sNewText = StringRegExpReplace($sText, "(?Um)^(\S*)\s(.*)\s(\S*)$", "$3 $2 $1")

; And here is the result
MsgBox($MB_SYSTEMMODAL, "New", $sNewText)

; Now rewrite the file with FileWrite

Decode:

(?Um)    - Non-greedy and multiline
^        - Start of line
(\S*)    - Caprture any non-spaces until
\s       - first space
(.*)     - Capture all characters until
\s       - last space
(\S*)    - Capture all nonspace characters until
$        - end of line

$3 $2 $1 - Reassmeble inserting spaces

M23

M23, thank you for the details explanation. This helps me learn as I go along so that soon I may be able to help others, just like you.

3 minutes ago, mikell said:

lostandconfused,
In Melba's code change to this line :

$sNewText = StringRegExpReplace($sText, "(?Um)^(.*)(\s)(.*)$", "$3$2$1")

 

This works. Thank you!

Link to comment
Share on other sites

  • Moderators

lostandconfused,

When you reply, please use the "Reply to this topic" button at the top of the thread or the "Reply to this topic" editor at the bottom rather than the "Quote" button - we know what we wrote and it just pads the thread unnecessarily.

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

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