Jump to content

StringRegExpReplace() Multi replace without two characters


Recommended Posts

Hi, how to multireplace in autoit without words with 2 characters?

Input:
aaa
%aaa%
bbb
%bbb%

Output:

xxx
%aaa%

xxx
%bbb%

StringRegExpReplace($sText, "[a],[b]","[x])

How??

@
 

I just want to not parse words containing % and :

Edited by MrKris1224
Link to comment
Share on other sites

Can you try to make your specification more precise please?

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

I want to replace the letters a-z to %chrset:~X, 1%
X = 0-24
Apart from the words containing the character % and :

Sorry i'am from poland it's google translator

 

Input:

@echo off ;replace this line
echo Hello World %asdas% ;replace this line without %asdas%

Output:

%chrset:~X, 1%%chrset:~X, 1%%chrset:~X, 1%%chrset:~X, 1%%chrset:~X, 1% etc.
%chrset:~X, 1%%chrset:~X, 1%%chrset:~X, 1%%chrset:~X, 1%%chrset:~X, 1%%asdas% etc.


 

Edited by MrKris1224
Link to comment
Share on other sites

If I understand you correctly you need to replace every word not enclosed with either % or : by a fixed string.

But what does  X = 0-24 mean?

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

Yes, I want to replace every word except the word with a "%" or ":"

But what does  X = 0-24 mean? - It's not important

Damn google translator xD

 

@

echo asdasda ; replace this line
:asasa ;no replace
%asasa% ; no replace

Edited by MrKris1224
Link to comment
Share on other sites

Does this work for you?

Local $s = "@echo off" & @CRLF & "echo Hello World %asdas%"
Local $res = StringRegExpReplace($s, "(?m)((?<!:|%)[@[:alpha:]]++(?!%)\h*)", "%chrset:~X, 1%")
ConsoleWrite($res & @LF)

BTW how can the output serve any purpose?

Edited by jchd

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

No, it's result:

%chrset:~X, 1%%chrset:~X, 1%
%chrset:~X, 1%%chrset:~X, 1%%chrset:~X, 1%%asdas%


Hmm...

Replace letters:

a = %chrset:~0, 1%

b = %chrset:~1, 1%

c = %chrset:~2, 1%

(...)

Words witch % or : character must be omitted

abc ;replace to %chrset:~0, 1%%chrset:~1, 1%%chrset:~2, 1%
abc %abc% ;replace to %chrset:~0, 1%%chrset:~1, 1%%chrset:~2, 1%%abc%
:abc ;don't replace

Link to comment
Share on other sites

I'm slowly getting it now: you whish to replace every letter within words not preceeded by : or not enclosed by % by the string "%chrset:~" concatenated with the index of the letter starting with a = 1  then concatenated with ", 1%".

BTW, there are 26 letters in a-z not 24 and how should we encode spaces, @ and other non-letters?

In other words: what are you trying to achieve? Is that some way of re-encoding commands into another codepage?

This is what I can do with your specifications as you exposed them:

Local $s = "@echo off" & @CRLF & "echo Hello World %asdas%"
Local $a = StringSplit($s, @CRLF, 3)
For $i = 0 To UBound ($a) - 1
    $a[$i] = Execute('"' & StringRegExpReplace($a[$i], "((?<!:|%)[@[:alpha:]]++(?!%)\h*)", '" & _LetterIdx("' & '\1' & '") & "') & '"')
Next
Local $res = _ArrayToString($a, @CRLF)
ConsoleWrite($res & @LF)

Func _LetterIdx($str)
    Local $a = StringToASCIIArray(StringLower($str))
    For $i = 0 To UBound($a) - 1
        $a[$i] = "%chrset:~" & ($a[$i] - Asc('a') + 1) & ", 1%"
    Next
    Return(_ArrayToString($a, ""))
EndFunc

Notice how @ and spaces are translated into negative values...

Edited by jchd

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

I'm slowly getting it now: you whish to replace every letter within words not preceeded by : or not enclosed by % by the string "%chrset:~" concatenated with the index of the letter starting with a = 1  then concatenated with ", 1%".

BTW, there are 26 letters in a-z not 24 and how should we encode spaces, @ and other non-letters?

In other words: what are you trying to achieve? Is that some way of re-encoding commands into another codepage?

This is what I can do with your specifications as you exposed them:

Local $s = "@echo off" & @CRLF & "echo Hello World %asdas%"
Local $a = StringSplit($s, @CRLF, 3)
For $i = 0 To UBound ($a) - 1
    $a[$i] = Execute('"' & StringRegExpReplace($a[$i], "((?<!:|%)[@[:alpha:]]++(?!%)\h*)", '" & _LetterIdx("' & '\1' & '") & "') & '"')
Next
Local $res = _ArrayToString($a, @CRLF)
ConsoleWrite($res & @LF)

Func _LetterIdx($str)
    Local $a = StringToASCIIArray(StringLower($str))
    For $i = 0 To UBound($a) - 1
        $a[$i] = "%chrset:~" & ($a[$i] - Asc('a') + 1) & ", 1%"
    Next
    Return(_ArrayToString($a, ""))
EndFunc

Notice how @ and spaces are translated into negative values...

Thank you. Were you able to almost achieve the expected goal.

Problem 1: The "eats" spaces

Problem 2: It does not have to be swapping characters to their counterparts in the Asc () only in numerical order in the alphabet - 1

a = 0 (in alphabet = 1)

b = 1

c = 2

d = 3

etc.

of course, with the annotation% chrset: ~ X, 1%

 

Damn google translator sorry for thiat

Link to comment
Share on other sites

Is that better?

Local $s = "@echo off" & @CRLF & "echo Hello World %asdas%"
Local $a = StringSplit($s, @CRLF, 3)
For $i = 0 To UBound ($a) - 1
    $a[$i] = Execute('"' & StringRegExpReplace($a[$i], "((?<!:|%)[[:alpha:]]++(?!%))", '" & _LetterIdx("' & '\1' & '") & "') & '"')
Next
Local $res = _ArrayToString($a, @CRLF)
ConsoleWrite($res & @LF)

Func _LetterIdx($str)
    Local $a = StringToASCIIArray(StringLower($str))
    For $i = 0 To UBound($a) - 1
        $a[$i] = "%chrset:~" & ($a[$i] - Asc('a')) & ", 1%"
    Next
    Return(_ArrayToString($a, ""))
EndFunc

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

may be to split the input file into words and check if it contains the character % or : , or if so, it normally prescribed and if this is not turning?

Ok it's work.

Now the question is whether we can do so that it was impossible to select a file to open on a computer and then replace in him should all this function ?? then save it with the name of the note _

For example:

File.bat> File_.bat

Edited by MrKris1224
Link to comment
Share on other sites

What exactly is not correct with the latest script?

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

You want to forbid use of the source bat and force redirect to the modified version? I don't know how you can do that, sorry.

Maybe change the registry value for .bat files and launch this script with a Run() to launch the modified .bat?

Edited by jchd

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

Meaning is lost in translation. Which country are you from? Maybe try posting a new thread with a title for catching attention of native speakers.

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

About half of my problem solved. Now the only question is why part of my letters somewhere disappears ??

Test this code:

#include <Array.au3>
Local $s = FileOpenDialog("Wybierz plik do kodowania",@DesktopDir,"Pliki języka batch (*.bat))
If $s = "" Then Exit
Local $a = StringSplit($s, @CRLF, 3)
For $i = 0 To UBound ($a) - 1
    $a[$i] = Execute('"' & StringRegExpReplace($a[$i], "((?<!:|%)[[:alpha:]]++(?!%))", '" & _LetterIdx("' & '\1' & '") & "') & '"')
Next
Local $res = _ArrayToString($a, @CRLF)
ConsoleWrite($res & @LF)
Func _LetterIdx($str)
    Local $a = StringToASCIIArray(StringLower($str))
    For $i = 0 To UBound($a) - 1
        $a[$i] = "%chrset:~" & ($a[$i] - Asc('a')) & ", 1%"
    Next
    Return(_ArrayToString($a, ""))
EndFunc
 
Most of the characters and the line disappears :(

Sorry for polish words in program.

Additionally, select the this file to "encode" and see the effects encoded, the program stops working

This file to "encode":

 

@echo off
:: Bardzo ważna linia \/
setlocal enabledelayedexpansion
:: wywołanie funkcji \/
call :split varka "teks,asdas fa,fasfa" ","
:: zwrot liczby słów do %varka0% oraz wyświetlenie wszystkich za pomocą for \/
For /l %%A in (1,1,%varka0%) do (echo.!varka%%A!)
pause>nul
exit
::Tutaj zaczyna się funkcja. call :split nazwa zmiennej tekst rozdzielacz
:split
set "var_name=%~1"
set vars=1
set chr=0
set text=%~2
set %var_name%0=1
set "delims=%~3"
:loop_split
set tmp=!text:~%chr%,1!
If not defined tmp (goto :EOF)
If "%tmp%"=="%delims%" (set/a vars+=1
set/a %var_name%0+=1)
If "%tmp%"=="%delims%" (set "%var_name%%vars%=") else (set "%var_name%%vars%=!%var_name%%vars%!%tmp%")
set/a chr+=1
goto loop_split
Edited by MrKris1224
Link to comment
Share on other sites

 Maybe write program in steps (sorry for my bad english):

1) Read source file
2) StringSplit using delimetrs " " (space)
3) Check word, if have chars: % or : then write normal word
4) If no have chars % or : then replace a-z to %chrset:~X,1%
5) if have other characters than a-z write other char normality
 

Link to comment
Share on other sites

That's because you specified only letters a-z. Polish language uses accents and these accented letters are stored as Unicode characters outside ASCII range when read from file.

Try with this modified version:

$a[$i] = Execute('"' & StringRegExpReplace($a[$i], "(*UCP)((?<!:|%)[\pL]++(?!%))", '" & _LetterIdx("' & '\1' & '") & "') & '"')

Notice that for example letter ł gets translated to "%chrset:~146, 1%".

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

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