Sign in to follow this  
Followers 0
shai

help with StringRegExpReplace

10 posts in this topic

i have text file like this:

2011-1-10 9:24:18> http://www.google.com/search?hl=en&safe=on&q=internet

2011-1-10 9:24:18> is a tata & time.

i want remove this (2011-1-10 9:24:18> ) string from line.

thanks

Share this post


Link to post
Share on other sites



I think StringTrimLeft will work

$NewString = StringTrimLeft("textfile.txt",19)

This should also remove the last space.


Get Scite to add a popup when you use a 3rd party UDF -> http://www.autoitscript.com/autoit3/scite/docs/SciTE4AutoIt3/user-calltip-manager.html

Share this post


Link to post
Share on other sites

Trimleft may have issues because the hour column changes from one to two digits

$string = "2011-1-10 9:24:18> http://www.google.com/search?hl=en&safe=on&q=internet"

$new = stringregexp ($string , "http://(.*)" , 1)

msgbox (0, '' , 'http://' & $new[0])

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

Share this post


Link to post
Share on other sites

shai,

StringTrimLeft will not work if the data/time has a varying number of characters: :P

2011-1-10 9:24:18> = 19 Chars

2011-11-30 15:24:18> = 21 chars

I believe you are correct to want to use a RegEx - this should work in all cases: :shifty:

$sText_1 = "2011-1-10 9:24:18> http://www.google.com/search?hl=en&safe=on&q=internet"
$sURL_1 = StringRegExpReplace($sText_1, "(?i).*?> (.*)", "$1")
ConsoleWrite($sURL_1 & @CRLF)

$sText_2 = "2011-11-30 15:24:18> http://www.google.com/search?hl=en&safe=on&q=internet"
$sURL_2 = StringRegExpReplace($sText_2, "(?i).*?> (.*)", "$1")
ConsoleWrite($sURL_2 & @CRLF)

Just to explain the SRE a bit:

(?i) = case insensitive

.*?> = A non-capturing group of the shortest run of characters ending in "> "

(.*) = A capturing group of the rest of the line

$1 = Replace with the first (and in this case only) capturing group

I hope all that makes sense - my brain starts to bleed when I deal with SREs. :x

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______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

 

Share this post


Link to post
Share on other sites

shai,

StringTrimLeft will not work if the data/time has a varying number of characters: :P

2011-1-10 9:24:18> = 19 Chars

2011-11-30 15:24:18> = 21 chars

I believe you are correct to want to use a RegEx - this should work in all cases: :shifty:

$sText_1 = "2011-1-10 9:24:18> http://www.google.com/search?hl=en&safe=on&q=internet"
$sURL_1 = StringRegExpReplace($sText_1, "(?i).*?> (.*)", "$1")
ConsoleWrite($sURL_1 & @CRLF)

$sText_2 = "2011-11-30 15:24:18> http://www.google.com/search?hl=en&safe=on&q=internet"
$sURL_2 = StringRegExpReplace($sText_2, "(?i).*?> (.*)", "$1")
ConsoleWrite($sURL_2 & @CRLF)

Just to explain the SRE a bit:

(?i) = case insensitive

.*?> = A non-capturing group of the shortest run of characters ending in "> "

(.*) = A capturing group of the rest of the line

$1 = Replace with the first (and in this case only) capturing group

I hope all that makes sense - my brain starts to bleed when I deal with SREs. :x

M23

ok its work perfect.

now if i have a file in desktop the name is "list_address.txt" and in this file i have multiline like this.

i want replace all lines and save the file after replaceing.

Share this post


Link to post
Share on other sites

shai,

Load the file into an array with _FileListToArray - loop through each line and apply the SRE - write the file back to disk with _FileWriteFromArray. Simple! :x

But be careful: the [0] index of the array is the count, so you will need to use the $i_Base parameter when you write the file. :P

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______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

 

Share this post


Link to post
Share on other sites

$sURL_1 = StringRegExpReplace($sText_1, "(?i).*?> (.*)", "$1")

You can simplify the regex:

$sURL_1 = StringRegExpReplace($sText_1, "[^>]*> ", "")

[^>]*> = A non-capturing group of the longest run of characters other than ">" ending in "> "

You don't need to worry about case since it will slurp up everything other than a ">" character.


Gerard J. Pinzonegpinzone AT yahoo.com

Share this post


Link to post
Share on other sites

M23,

I'm with ya on that "brain bleedin" thing...

Do these examples assume that there are no ">"'s in the target text?

kylomas


Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Share this post


Link to post
Share on other sites

#9 ·  Posted (edited)

GPinzone,

Thanks for that. Always happy to learn about SREs - I make no pretence of being expert at them! :P

M23

Edit: Actually, always happy to learn about anything! :x

Edited by Melba23

Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______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

 

Share this post


Link to post
Share on other sites

#10 ·  Posted (edited)

If you use (?m) you can parse the entire string line by line (delimited by line feeds or carriage returns) so you do not have to read the file into an array

Local $String = FileRead('YourFile.txt')
If Not $String And Not @error Then Exit
MsgBox(0, 'Before', $String)
$String = StringRegExpReplace($String, '(?m)^.*> *', '')
MsgBox(0, 'After', $String)

Edited by Varian

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