Jump to content

Help with Regexp String.


Recommended Posts

Hi all, 

I've been struggling with this regular expression for a while now, so i'm hoping someone can point me in the right direction..

My script is reading lines from a text file.

As an example, here is one of the strings (that i've been working on - trying to get the correct result)

Some details have been altered for privacy reasons..

Local $results
; This is an example of the string i'm trying to manipulate....
$InfoMatch = "solrs013,121,192.168.1.0/26,r_Labelsolsw-b96m-02,solsw-b96m-01,DEFsolsw-b96l-001"

$sString = TrimSwitchDetails($InfoMatch)
ConsoleWrite($sString & @CRLF)

; I Want to be left with an array of solsw-b96m-02 , solsw-b96m-01 , solsw0b96l-001 (less the seperating commas)

Func TrimSwitchDetails($results) ; REGEXP Function - This locates all New Naming Convention Access Switches.
    Local $match = StringRegExp($results, '(.*?)(\D{5})(\W)(\w{4})(\W)(\d{2,3})', 3)
;~  Local $match = StringRegExp($results, '(.*?)(....)([solsw])(\W)(\w{4})(\W)(\d{2,3})', 3) <-- Tried this way too..
    Local $result = ''
    If IsArray($match) Then
        For $I = 1 To UBound($match) - 1
            $result &= $match[$I]
        Next
    EndIf
    Return $result
EndFunc   ;==>TrimSwitchDetails

I'm new to Regexp, but i've muddled my way through and got several Regexp functions working by reading through the Helpfile (ie. filtering out the IP Address from above string)

But, i can't  leave myself with the three switch names in the array. solsw-b96m-02 / solsw-b96m-01 / solsw-b96l-001 and leave behind the addition letters that i find are being added to the start of some the switches.

I'd also like to point out, there could be one switch name in the string, or there could be 10 names that need pulling out, which unless i'm doing it all wrong (again), is why i'm using the '3' flag.

Any help would be much appreciated.

Thanks in anticipation..

Link to comment
Share on other sites

Hi.

For me this is working:

(solsw-[a-z0-9]{4,}-\d+)

If there are examples outside this some points can be modified. Conrad

SciTE4AutoIt = 3.7.3.0   AutoIt = 3.3.14.2   AutoItX64 = 0   OS = Win_10   Build = 19044   OSArch = X64   Language = 0407/german
H:\...\AutoIt3\SciTE     H:\...\AutoIt3      H:\...\AutoIt3\Include     (H:\ = Network Drive)

   88x31.png  Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind.

Link to comment
Share on other sites

  • Moderators

vestanpance3110,

Welcome to the AutoIt forums.

This seems to work:

#include <Array.au3>

$InfoMatch = "solrs013,121,192.168.1.0/26,r_Labelsolsw-b96m-02,solsw-b96m-01,DEFsolsw-b96l-001"

$aRet = StringRegExp($InfoMatch, "(\w{5}-b\d\d\w-\d*)", 3)

_ArrayDisplay($aRet, "", Default, 8)

Please ask if you have any questions, although as I am not a RegExp guru I cannot guarantee an answer!

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

@Melba23s answer is working too. It's a decision between as special as possibly (to not match false) and as general as needed (to catch all you want). 

Questions: 

1. Start they always with "solsw-b"?

2. Always followed by 2 digits and one letter or digit?

3. Always followed by "-"?

4. Always followed by only digits? And how many of them?

Conrad

Edited by Simpel
Typo
SciTE4AutoIt = 3.7.3.0   AutoIt = 3.3.14.2   AutoItX64 = 0   OS = Win_10   Build = 19044   OSArch = X64   Language = 0407/german
H:\...\AutoIt3\SciTE     H:\...\AutoIt3      H:\...\AutoIt3\Include     (H:\ = Network Drive)

   88x31.png  Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind.

Link to comment
Share on other sites

Hi M23 and Conrad, 

I'd just like to start off by saying thank you to you both for your speedy replies to my query. :thumbsup:

Thanks also to M23 for the warm welcome to the forum. I have seen you solve many a problem in the forum over the years and your assistance to others has helped me a great deal in understanding / solving my own issues. (Once i've gotten my head around what you did to solve 'their' problems... :D)

I'm very grateful for the regex's you both supplied, but i also have to confess, i was going about things the wrong way.. I spent so long and got so frustrated with not being able to remove the characters from the switch names (hence creating an account) that i hadn't thought about HOW they were getting there in the first instance..
Once i had removed myself from the problem (awaiting a forum reply) when i went back, it occurred to me i was doing things 'arse about face' and have resolved the issue by stopping the characters from appearing in the first place by correcting the relevant regexp..

I've heard M23 time and again say that Regexp is a language in it's own right! but i'd like to understand what was happening with them if that's okay.

I more or less understand M23's (with maybe the exception of the -b

Conrad's, i'm not completely sure about...

(solsw-[a-z0-9]{4,}-\d+)

; solsw- = Prefix of string
; [a-z0-9] = any letter or number from A to Z , 1-9?
; {4,} = Only allowing 4 letters/numbers in the sequence?
; - = Another dash. So is it possible to specify a 'non word character?
; \d+ = I think i understand this is Digits only, but what does the + do?

I'm currently re-working the next section of my script, wherein i'm comparing two files in a For /Next loop, replacing ethernet interfaces with switch names, which is giving me some grief, but i'll stick with it for now..

Melba has helped someone doing something very similar, but reading both files into arrays and processing it in memory. 

It's interesting! but i can't get it to work. I might have to raise another topic.. ;)

Thanks again guys, your help and response was much appreciated.

Link to comment
Share on other sites

Hi @vestanpance3110,

you are right with your explanation. Two things more:

- {4,} means four or more; if you want exactly four it's {4}

- you can use \W a non word symbol to find the dash; I prefer more precise the symbol itself if I'm sure it's matching always

- \d+ means one until unlimited digits; if you know the count do it more exact as above written

Testing regexes I use the page regex101.com. You can find and play around with your case here: https://regex101.com/r/TTlwz3/2

Regards, Conrad 

SciTE4AutoIt = 3.7.3.0   AutoIt = 3.3.14.2   AutoItX64 = 0   OS = Win_10   Build = 19044   OSArch = X64   Language = 0407/german
H:\...\AutoIt3\SciTE     H:\...\AutoIt3      H:\...\AutoIt3\Include     (H:\ = Network Drive)

   88x31.png  Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind.

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