Jump to content

StringRegExpReplace help


Recommended Posts

$sMAC = "333311116666"

If StringRegExp($sMAC, "[0-9]") Then $sMac = StringRegExpReplace($sMAC, "[0-8]", "A", Random(1, 12))
ConsoleWrite($sMAC & @CR)

Trying to accomplish the following.

If $sMAC contains only digits between 0-9 then replace a random number of those digits with the following characters A through F.

I'm not sure how to accomplish the last bit. How do I have it choose a different letter for each replacement based on a pattern of available letters A through F?

Link to comment
Share on other sites

$sMAC = "333311116666"
$max = stringlen ($sMAC)
If StringRegExp($sMAC, "[0-9]") Then
for $i = 1 to random(1, $max , 1)
$sMAC = stringreplace($sMAC , random(1, $max , 1) , Chr(Random(Asc("A"), Asc("F"), 1)))
next
Endif

ConsoleWrite($sMAC & @CR)

*fixed should match behavior described

Edited by boththose

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

Link to comment
Share on other sites

Are you trying to create a random MAC address? I'm pretty sure the site owner would not find that acceptable.

Yes.

We name our computers according to their MAC address when imaging. However some MAC's contain only numbers which is against MS best practices when naming computers.

Thus I am trying to randomize the MAC/Computer name so we don't get duplicates during imaging and follow MS best practices.

Link to comment
Share on other sites

We name our computers according to their MAC address when imaging. However some MAC's contain only numbers which is against MS best practices when naming computers.

Thus I am trying to randomize the MAC/Computer name so we don't get duplicates during imaging and follow MS best practices.

Ah! I got the wrong impression for a moment. I thought you were up to no good. I apologies for that.

I have always found avoiding duplicates when using random selection to be complicated. There are many possible permutations. Perhaps you could stick to only using values within a certain range or select a random smaller set from several ranges as candidates. No matter what you do, you will have to test the random addresses against those which don't need to be changed - unless you change them all. Making the numbers smaller is one way to simplify the problem. I need to think about this.

Edit

I calculate 280474976710657 permutations with a minimum of one letter in each 12 character address (unless I made a mistake).

Edited by czardas
Link to comment
Share on other sites

$sMAC = "333311116666"
$max = stringlen ($sMAC)
If StringRegExp($sMAC, "[0-9]") Then
for $i = 1 to random(1, $max , 1)
$sMAC = stringreplace($sMAC , random(1, $max , 1) , Chr(Random(Asc("A"), Asc("F"), 1)))
next
Endif

ConsoleWrite($sMAC & @CR)

*fixed should match behavior described

Thank you this works. wonder how I can tweak it. I'm reading about stringreplace and it has an optional flag of "occurrence" which I'm trying to set. I'd like to only replace 8 out of the maximum 12 digits, but only the last 8... never the first 4. Thus I was trying to make an occurrence of "-8" which by the helpdoc should start the checking from the right side, but it's not working like it should.

EDIT: got it. instead of using occurrence I just set the Random start searchstring to 5, 12, 1. Thanks!

Edited by kor
Link to comment
Share on other sites

Here another way:

Global $sMAC_New
$sMAC = "333311116666"
$aArr = StringSplit($sMAC, "", 2)
For $i = 0 To UBound($aArr) - 1
    If StringIsInt($aArr[$i]) Then $aArr[$i] = Chr(Random(65, 70, 1))
    $sMAC_New &= $aArr[$i]
Next
MsgBox(0, "Test", $sMAC_New)

Or:

Global $sMAC_New
$sMAC = "12345678"
For $i = 1 To StringLen($sMAC)
    If StringIsInt(StringMid($sMAC, $i, 1)) Then
        $sMAC_New &= Chr(Random(65, 70, 1))
    Else
        $sMAC_New &= StringMid($sMAC, $i, 1)
    EndIf
Next
MsgBox(0, "Test", $sMAC_New)

Br,

UEZ

Edited by UEZ

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Link to comment
Share on other sites

Another approach: pseudo-randomize all MAC addresses since they mean nothing to you, but create them in sequence.

If you want to satisfy the requirement that MAC addresses have at least one hex digit yet be unique, use the following pseudocode:

Global $StationNumber = 12345  ; start with some non-negative value less than 2^63-1 - Round(((<expected number of stations> + 4) / 5) * 16)
Local $MAC
For <every station>
If Mod($StationNumber, 16) = 0 Then $StationNumber += 10
$MAC = Hex($StationNumber, 12) ; 12 digits in string, but last one (right) is in A-F
$StationNumber += 1 ; increment for next station
Next

Note: I choose to make the last hex digit in A-F, but you can easily adapt the idea to any other digit.

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

In the OP you say each replacement must be different. I presume by that you mean that the same letter may not appear twice in the last 8 characters.

No, you can repeat. That is the reason for the attempt at randomness in the replacements. That is also why I don't want to replace all the digits, just the last 8. This is a special case since not a lot of MAC address have only numbers, but my script must account for those rare machines.

I figure the percentage of MACs that contain only digits is low. Then add to that a randomized replacement of A-F on a randomized number of 1-8 digits should reduce the possability of a duplicate down to something so small it's not a worry.

Link to comment
Share on other sites

Posted Image

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

Link to comment
Share on other sites

No, you can repeat.

It was the following statement that threw me.

How do I have it choose a different letter for each replacement based on a pattern of available letters A through F?

I figure the percentage of MACs that contain only digits is low. Then add to that a randomized replacement of A-F on a randomized number of 1-8 digits should reduce the possability of a duplicate down to something so small it's not a worry.

Hmm, that's not water-tight. The chance of a repeat is also affected by the distribution of digits prior to replacement. If you think it's an acceptable risk, then okay. Edited by czardas
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...