Jump to content

Replace Alpha-Numeric ID


Recommended Posts

I want to replace an alpha-numeric ID wherever it occurs, but not if it's a sub-part of another.  In the example the first 7 should be replaced, but not the rest.  The code identifies the right items, but the replace includes the characters either side.  Is there a way to get StringRegExpReplace to do this?

$sInput = " 1 A1 2 :A1 3 +A1 4 -A1 5 A1: 6 A1; 7 ;A1 8 3A10 9 :A10 10  AA1 11 A10 "
$sOutput = StringRegExpReplace($sInput, "[^A-Z,0-9]A1[^A-Z,0-9]", "--")
ConsoleWrite(StringFormat("In   %s\nOut  %s\n", $sInput, $sOutput))
In    1 A1 2 :A1 3 +A1 4 -A1 5 A1: 6 A1; 7 ;A1 8 3A10 9 :A10 10  AA1 11 A10 
Out   1--2 --3 --4 --5-- 6-- 7 --8 3A10 9 :A10 10  AA1 11 A10

If that's not possible I'll find each "A1" then check the characters each side.  Not difficult but not as nice as a RegExp method.

Link to comment
Share on other sites

I don't understand the precise logic you use to derive the output in your sample, mainly w.r.t. whitespaces. All I can easily come up with is this:

$sInput = " 1 A1 2 :A1 3 +A1 4 -A1 5 A1: 6 A1; 7 ;A1 8 3A10 9 :A10 10  AA1 11 A10 "
$sExpected = " 1--2 --3 --4 --5-- 6-- 7 --8 3A10 9 :A10 10  AA1 11 A10"
$sOutput = StringRegExpReplace($sInput, "(?:[-+:;\s]?)(\bA1[:;]?(?=\W)\s?)", "--")
ConsoleWrite(StringFormat("In   %s\nOut  %s\nExp  %s\n", $sInput, $sOutput, $sExpected))

which gives that:

In    1 A1 2 :A1 3 +A1 4 -A1 5 A1: 6 A1; 7 ;A1 8 3A10 9 :A10 10  AA1 11 A10 
Out   1--2 --3 --4 --5--6--7 --8 3A10 9 :A10 10  AA1 11 A10 
Exp   1--2 --3 --4 --5-- 6-- 7 --8 3A10 9 :A10 10  AA1 11 A10

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

My regexp is using the chars each side of the A1 to see that it's not part of a bigger ID, so those two chars get replaced, so losing spaces or + or - whatever.  So I want no loss of spaces or other chars, just A1 replaced with --.  I don't know the complete set of chars that it might be either side of A1, so have to use not A-Z.

Link to comment
Share on other sites

Is that better?

$sInput =    " 1 A1 2 :A1 3 +A1 4 -A1 5 A1: 6 A1; 7 ;A1 8 3A10 9 :A10 10  AA1 11 A10 "
$sExpected = " 1 -- 2 :-- 3 +-- 4 --- 5 --: 6 --; 7 ;-- 8 3A10 9 :A10 10  AA1 11 A10"
$sOutput = StringRegExpReplace($sInput, "(?<![A-Z0-9])A1(?![A-Z0-9])", "--")
ConsoleWrite(StringFormat("In   %s\nOut  %s\nExp  %s\n", $sInput, $sOutput, $sExpected))

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