Jump to content

help with StringRegExpReplace


shai
 Share

Recommended Posts

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])

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

Link to comment
Share on other sites

  • Moderators

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

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

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.

Link to comment
Share on other sites

  • Moderators

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

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

$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
Link to comment
Share on other sites

  • Moderators

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

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

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