Jump to content

StringRegExReplace


Recommended Posts

Hello,

i have a huge amount of data to process. My goal is it to turn this

This is a text @1234@ seperated by some @7890@ strange looking @8755@ constructions.

into this

This is a text @1@ seperated by some @2@ strange looking @3@ constructions.

I am able to replace the @####@ data with this piece of code:

$sString = "This is a text @1234@ seperated by some @7890@ strange looking @8755@ constructions."
$sResult = StringRegExpReplace($sString, "@[0-9]{4}@", "\n", 0)

Is there anyway to achieve my goal using StringRegExReplace?

Thanks for some hints.

Link to comment
Share on other sites

  • Moderators

HurleyShanabarger,

Welcome to the AutoIt forums. :)

I fear you may have to do some additional processing: :(

#include <StringConstants.au3>

$sString = "This is a text @1234@ seperated by some @7890@ strange looking @8755@ constructions."

; Replace all instances of "@nnnnn@" with a single unique string
$sReplaced = StringRegExpReplace($sString, "@[0-9]{4}@", "@#####@", 0)

; Split the string on that unique string
$aSplit = StringSplit($sReplaced, "@#####@", $STR_ENTIRESPLIT)

; Reconstruct the string adding the increasing count value
$sResult = ""
For $i = 1 To $aSplit[0] - 1
    $sResult &= $aSplit[$i] & "@" & $i & "@"
Next
$sResult &= $aSplit[$i]

ConsoleWrite($sResult & @CRLF)
But no doubt some RegEx guru will be along in a minute to show us mere mortals how it can be done. :D

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

The specification is ambiguous. Do you want to replace successive occurrences of @dddd@ by successive values @1@, @2@, @3@, ... like mikell did

or do you pick digits from the first occurrence for replacement?

In other words, what should be the outcome of:

This is a text @6543@ seperated by some @7890@ strange looking @8755@ constructions.

This?

This is a text @1@ seperated by some @2@ strange looking @3@ constructions.

That?

This is a text @6@ seperated by some @5@ strange looking @4@ constructions.

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

Also,

Does a "@" always delimit the chars you want to replace?

Is the replaced pattern always 4 digits?

edit:

A real life example (sample of your file) might b helpful.

Edited by 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

Link to comment
Share on other sites

Hey guys,

I got another one for you. The data that I want to examine now is build similar like this:

<field XXXXXXX>Text<More nested stuff></field><field YYYYYY>test</field>

What i want to retrieve now is this:

[0] = <field XXXXXXX>Text<More nested stuff></field>
[1] = <field YYYYYY>test</field>

I tried it with this piece of code, but that is not helping me:

$_l_sString = '<field XXXXXXX>Text<More nested stuff></field><field YYYYYY>test</field>'
$_l_aResult = StringRegExp($_l_sString , "(?<=\<field)(.*)(?=\<\/field>)", 1)

Any hints for me?

Thanks again!

Link to comment
Share on other sites

That?

#include <Array.au3>

$_l_sString = '<field XXXXXXX>Text<More nested stuff></field><field YYYYYY>test</field>'
$_l_aResult = StringRegExp($_l_sString , "(<field.*?/field>)", 3)
_ArrayDisplay($_l_aResult)

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

@Mikell : the OP said i have a huge amount of data to process, so maybe it's possible to have more than 999 replacements. In this case, your code won't work properly. :laser:

But Melba's code will do the job perfectly. :P

Sorry for the joke ... Just add a replacement to avoid this (a mix of Melba's and Mikell's code):

$sString = "This is a text @1234@ seperated by some @7890@ strange looking @8755@ constructions."

$n = 0
While 1
    $n += 1
    $sString = StringRegExpReplace($sString, "(?<=@)\d{4}(?=@)", "#" & $n & "#", 1)
    If @extended = 0 Then Exitloop
Wend
$sString = StringRegExpReplace($sString, "@#(\d+)#@", "@$1@")

Msgbox(0,"", $sString)
Edited by jguinch
Link to comment
Share on other sites

I tried to launch the replacement with my code with a file containing 10000 lines : it's very slow :(

With this following code, it's really faster (just one global remplacement and one Execute) :

$sString = "This is a text @1234@ seperated by some @7890@ strange looking @8755@ constructions."

$sOutput = "'" & StringReplace($sString, "'", "''") & "'"
$sOutput = Execute ( StringRegExpReplace($sOutput  , "(?<=@)\d+(?=@)", "' & Assign(""iReplace"", Eval(""iReplace"") + 1) * 0 + Eval(""iReplace"") & '") )
MsgBox(0, "", $sOutput)
Edited by jguinch
Link to comment
Share on other sites

I doubt I'm at the root of Execute(StringRegexp...), in AutoIt ot whatever other language. We just borrow/inherit from piles of geniuses.

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