Sign in to follow this  
Followers 0
ethneldryt

Help for StringRegExpReplace

23 posts in this topic

Hello,

I need to remove the space of a variable with StringRegExpReplace or others.

I'm bad at regexp

Thx :)

exemple :

$var = "hello world"

result : "helloworld"

Share this post


Link to post
Share on other sites



Use this:

StringRegExpReplace("Hello World", "[ ]", "")

[font="Garamond"]Manjish Naik[/font]Engineer, Global Services - QPSHoneywell Automation India LimitedE-mail - Manjish.Naik@honeywell.com

Share this post


Link to post
Share on other sites

It does not work :s

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

What r u talking about..

It works perfectly well..

MsgBox(0, "Regular Expression Replace Test", StringRegExpReplace("Hello World", "[ ]", ""))
Edited by Manjish

[font="Garamond"]Manjish Naik[/font]Engineer, Global Services - QPSHoneywell Automation India LimitedE-mail - Manjish.Naik@honeywell.com

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

Hey

this should do the trick,

$sString = "    Hello World!"
$sString = StringRegExpReplace($sString, "\s", "")
MsgBox(0, "String without spaces", $sString)

\s matches any type of space

Edited by Robjong

Share this post


Link to post
Share on other sites

Why use RegExp, when you can just use StringStripWS...

$sVar = "hello world"
$sResult = StringStripWS($sVar, 8)

?

:)


 

Spoiler

Using OS: Win 7 Professional, Using AutoIt Ver(s): 3.3.6.1 / 3.3.8.1

AutoIt_Rus_Community.png AutoIt Russian Community

My Work...

Spoiler

AutoIt_Icon_small.pngProjects: ATT - Application Translate Tool {new}| BlockIt - Block files & folders {new}| SIP - Selected Image Preview {new}| SISCABMAN - SciTE Abbreviations Manager {new}| AutoIt Path Switcher | AutoIt Menu for Opera! | YouTube Download Center! | Desktop Icons Restorator | Math Tasks | KeyBoard & Mouse Cleaner | CaptureIt - Capture Images Utility | CheckFileSize Program

AutoIt_Icon_small.pngUDFs: OnAutoItErrorRegister - Handle AutoIt critical errors {new}| AutoIt Syntax Highlight {new}| Opera Library! | Winamp Library | GetFolderToMenu | Custom_InputBox()! | _FileRun UDF | _CheckInput() UDF | _GUIInputSetOnlyNumbers() UDF | _FileGetValidName() UDF | _GUICtrlCreateRadioCBox UDF | _GuiCreateGrid() | _PathSplitByRegExp() | _GUICtrlListView_MoveItems - UDF | GUICtrlSetOnHover_UDF! | _ControlTab UDF! | _MouseSetOnEvent() UDF! | _ProcessListEx - UDF | GUICtrl_SetResizing - UDF! | Mod. for _IniString UDFs | _StringStripChars UDF | _ColorIsDarkShade UDF | _ColorConvertValue UDF | _GUICtrlTab_CoverBackground | CUI_App_UDF | _IncludeScripts UDF | _AutoIt3ExecuteCode | _DragList UDF | Mod. for _ListView_Progress | _ListView_SysLink | _GenerateRandomNumbers | _BlockInputEx | _IsPressedEx | OnAutoItExit Handler | _GUICtrlCreateTFLabel UDF | WinControlSetEvent UDF | Mod. for _DirGetSizeEx UDF
 
AutoIt_Icon_small.pngExamples: 
ScreenSaver Demo - Matrix included | Gui Drag Without pause the script | _WinAttach()! | Turn Off/On Monitor | ComboBox Handler Example | Mod. for "Thinking Box" | Cool "About" Box | TasksBar Imitation Demo

Like the Projects/UDFs/Examples? Please rate the topic (up-right corner of the post header: Rating AutoIt_Rating.gif)

* === My topics === *

==================================================
My_Userbar.gif
==================================================

 

 

 

AutoIt is simple, subtle, elegant. © AutoIt Team

Share this post


Link to post
Share on other sites

i want to replace a double quote " in a script

script is a directory path passed to the script as CmdLine[1]

so e.g

CmdLine[1] is "c:\blah\blue"

i want to remove double quote and keep rest of path c:\blah\blue

$sStrippedPend1 = StringReplace($CmdLineRaw, "["]", "")

or

$sStrippedPend1 = StringReplace($CmdLineRaw, """, "")

don't work because " is seen as script language (unterminated script)

what's the solution?.. thanks

Share this post


Link to post
Share on other sites

used stringtrimright and left to remove double quotes..it worked

$line=FileReadLine ($CmdLine[1], 2 )

$sStrippedCmd = StringRegExpReplace($line, "URL=", "")

$sStrippedPend1 = StringReplace($CmdLineRaw, "E:\briefcase\", "")

$sStrippedPend2 = StringReplace($sStrippedPend1, "HE-pending\", "")

$sStrippedPend3 = StringTrimLeft($sStrippedPend2, 1)

$sStrippedName = StringTrimRight($sStrippedPend3, 1)

Share this post


Link to post
Share on other sites

a) when running from the command line the parameters already get the quotes striped from them unless there are extra quotes being added

b)stringreplace seems more simple unless there are quotes in the param

$a='"asdfasdf asdfasdf"'

MsgBox(0,'',StringReplace($a,'"',''))

Share this post


Link to post
Share on other sites

Ok, so here is an even more n00b question. For the life of me I can't figure out how to replace an arbitrary text between two known strings. I have a text file with multiple lines and know that the text I want to replace starts with string a and ends with string b on a different line further down. I want to replace the text including string a and b.

I know this must be trivial, but I just spent the better part of an hour browsing the forums for a solution...Any help is greatly appreciated!

Share this post


Link to post
Share on other sites

#11 ·  Posted (edited)

Hey,

this should get you started

$sString = "I am a string with some arbitrary " & @CRLF & _
        "text that needs to be replaced, " & @CRLF & _
        "this part needs to remain in place." & @CRLF


$sString = StringRegExpReplace($sString, "(?i)(?s)(.*)arbitrary.*?replaced(.*)", "\1new text\2")
ConsoleWrite($sString & @CRLF)

"(?i)(?s)(.*)arbitrary.*?replaced(.*)"

(?i) - case insensetive

(?s) - . also matches new lines

(.*) - captures the text from beginning up to "arbitrary"

arbitrary- the known word the string we want to replace starts with

.* - matches anything (including newlines)

? - tells the engine to get the smallest match instead of the largest (if replaced would be in the text more then once after the starting word the first one is used)

replaced- the known word the string we want to replace ends with

(.*) - captures the text from "replaced" to the end

"\1new text\2"

\1 - backreference to the first captured text

new text - our new replacing text

\2 - backreference to the second captured text

Edited by Robjong

Share this post


Link to post
Share on other sites

Ok, so here is an even more n00b question. For the life of me I can't figure out how to replace an arbitrary text between two known strings. I have a text file with multiple lines and know that the text I want to replace starts with string a and ends with string b on a different line further down. I want to replace the text including string a and b.

I know this must be trivial, but I just spent the better part of an hour browsing the forums for a solution...Any help is greatly appreciated!

And this appears to work also.

Local $sFileName = "Path\NameOfFile.txt"
Local $sStrA = "first string"
Local $sStrB = "second string"
Local $sStrReplace = "HERE IS THE REPLACEMENT STRING"
Local $sREResult = StringRegExpReplace(FileRead($sFileName), "(?s)" & $sStrA & "(.*)" & $sStrB, $sStrReplace)

; Saves to same file or could save to another file if required.
$hFile = FileOpen($sFileName, 2)
FileWrite($hFile, $sREResult)
FileClose($hFile)

Share this post


Link to post
Share on other sites

Guys, you are amazing. Thanks so much, you saved my weekend.

BTW: Can you point me towards a good tutorial for StringRegExpReplace, if possible one with example scripts, I'm afraid I couldn't really wrap my head around the rather abstract description in the AutoIt help file.

Share this post


Link to post
Share on other sites

#15 ·  Posted (edited)

Hey,

There is a link with detailed information in the StringRegExp help info,

http://www.autoitscript.com/autoit3/pcrepattern.html

Not sure if there is more about StringRegExpReplace but, AutoIt uses PCRE (Perl Compatible Regular Expressions)

so a search for "PCRE tutorial" will get you started :)

http://www.google.com/search?q=PCRE+tutorial

its not that difficult, so i suggest you just open up some tutorial/documentation and play with it a bit

Edit: rephrased

Edited by Robjong

Share this post


Link to post
Share on other sites

Thanks, as usual the first google hit helped a lot, if anyone else looks for the same: http://www.regular-expressions.info/quickstart.html

However, I have now hit the next wall: I have a text file in which various information is grouped for different entries, but not every entry has all the information, eg

file 1

application date 19/03/2003

recordal date 20/03/2003

file 2

application date 30/12/2007

file 3

application date 10/02/2005

recordal date 20/10/2005

I need to get this into a csv file. I have tried something along the following lines using ? to make the whole recordal thing optional, which did not work (found no matches):

$text = StringRegExpReplace($text, "(?i)(?s)application date (\d\d\.\d\d.\d\d\d\d).*?(recordal date (\d\d\.\d\d.\d\d\d\d))?", "\1,\3")

And being at it: Is there a possibility to name groups like in Perl (?P<name>group), I couldn't seem to make that work.

Thanks a lot for your help!

Share this post


Link to post
Share on other sites

#17 ·  Posted (edited)

Can you give more information about what file, for example, might contain other than generic "file1".

All I can think of right now is that there is no need for regular expression, only FileRead and StringSplit.

Edit:

#include <Array.au3>

$s = 'The the'
$a = StringRegExpReplace($s, '(?i)(?P<NAME>\w+) \b(?P=NAME)\b', '$1')
ConsoleWrite(@error & @TAB & @extended & @CRLF & $a & @CRLF)

As long as it's a valid pattern I see it does support naming groups.

Edited by Authenticity

Share this post


Link to post
Share on other sites

#18 ·  Posted (edited)

Can you give more information about what file, for example, might contain other than generic "file1".

All I can think of right now is that there is no need for regular expression, only FileRead and StringSplit.

Edit:

#include <Array.au3>

$s = 'The the'
$a = StringRegExpReplace($s, '(?i)(?P<NAME>\w+) \b(?P=NAME)\b', '$1')
ConsoleWrite(@error & @TAB & @extended & @CRLF & $a & @CRLF)

As long as it's a valid pattern I see it does support naming groups.

Sorry, bad description on my part. It is not actually a separate file, it is just a couple of lines in one big file. Where it says "file 1", read "entry 1".

Thanks for the naming groups description. i wonder what I did wrong, I'll work it out.

Edited by verris

Share this post


Link to post
Share on other sites

#include <Array.au3>

$s = 'file 1' & @CRLF & _
     'application date 19/03/2003' & @CRLF & _
     'recordal date 20/03/2003' & @CRLF & _
     'file 2' & @CRLF & _
     'application date 30/12/2007' & @CRLF & _
     'Some non catched text' & @CRLF & _
     'file 3' & @CRLF & _
     'application date 10/02/2005' & @CRLF & _
     'recordal date 20/10/2005'
     
Dim $sPattern = '(file[^\r\n]*\r\n)(application[^\r\n]*\r\n)?(recordal[^\r\n]*\r\n)?'
Dim $aResults = StringRegExp($s, $sPattern, 3)

If IsArray($aResults) Then _ArrayDisplay($aResults)

Share this post


Link to post
Share on other sites

Hey,

this might be easyer

#include <Array.au3>


$sString = "file 1" & @CRLF & "application date 19/03/2003" & @CRLF &  "recordal date 20/03/2003" & @CRLF & _
     "file 2" & @CRLF & "application date 30/12/2007" & @CRLF & "Some non catched text" & @CRLF & _
     "file 3" & @CRLF & "application date 10/02/2005" & @CRLF & "recordal date 20/10/2005"


$aRes = StringRegExp($sString, "(?i)((?:file|recordal|application)[^\r\n]+)", 3)
_ArrayDisplay($aRes)

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