Jump to content

StringRegExp


Recommended Posts

hi everyone,

i am very confused on using the StringReg Exp function.

how do i strip thw following

172.17.153.7 2008 Jan 14 06:57:02 SIN +08:00 %CDP-4-NVLANMISMATCH:Native vlan mismatch detected on port 3/6

to this

172.17.153.7 CDP-4-NVLANMISMATCH:Native vlan mismatch detected on port 3/6

thank you!

Link to comment
Share on other sites

$s_Str = "172.17.153.7 2008 Jan 14 06:57:02 SIN +08:00 %CDP-4-NVLANMISMATCH:Native vlan mismatch detected on port 3/6"
$s_Rtn = StringRegExpReplace($s_Str, "((25[0-4]|(2[0-4]|1[0-9]|[1-9]?)[0-9]\.){3}(25[0-4]|(2[0-4]|1[0-9]|[1-9]?)[0-9])\h+).*?%(.+)", "$1$6")
MsgBox(0, "Result", $s_Rtn)

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Link to comment
Share on other sites

There is any guide on autoit reg exp?

I just tried with help file and some selfmade code but got nothing...

I know a little about reg exping in some other language but when I get into trying here in autoit the output is always blank or 0.

I'm a compulsive poster. When I post something, come to read it at least 5 minutes later after the posting, because I will edit it. I edited even this signature a few minutes later after I wrote it.

Link to comment
Share on other sites

Here is an example of some code working on other lines.

Local $str = "172.17.153.7 2008 Jan 14 06:57:02 SIN +08:00 %CDP-4-NVLANMISMATCH:Native vlan mismatch detected on port 3/6" & @CRLF & _
            "169.17.154.8 2011 Aug 12 11:00:10 SIN +08:00 %CUP-5-NVLANMISMATCH:Native vlan mismatch detected on port 2/5"

ConsoleWrite(StringRegExpReplace($str, "(\d{4} [[:alpha:]]{3}.*? %)", "") & @LF)

; The RE pattern, "(\d{4}.*? %)", matches 4 digits, followed by space, then at least 3 letters, followed by all characters up
; to and including the first "space%" characters that are encountered.   All those matched characters are then replaced
; with "", which is nothing.
;
; So everything between "4 digits, a space, and 3 letters" and ending with "a space and %", inclusive, is deleted.
; Returns:-
; 172.17.153.7 CDP-4-NVLANMISMATCH:Native vlan mismatch detected on port 3/6
; 169.17.154.8 CUP-5-NVLANMISMATCH:Native vlan mismatch detected on port 2/5

@Newb

If you are using StringRegExp() function and you don't know an array is returned, this would explain why a blank is always returned. See "Flag" parameter in help file under StringRegExp. Otherwise, it may be best to start your own topic.

Link to comment
Share on other sites

There is any guide on autoit reg exp?

I just tried with help file and some selfmade code but got nothing...

I know a little about reg exping in some other language but when I get into trying here in autoit the output is always blank or 0.

Look here.

AutoIt 3.3.14.2 X86 - SciTE 3.6.0WIN 8.1 X64 - Other Example Scripts

Link to comment
Share on other sites

Here my version:

$input = "172.17.153.7 2008 Jan 14 06:57:02 SIN +08:00 %CDP-4-NVLANMISMATCH:Native vlan mismatch detected on port 3/6"
MsgBox(0, "RegEx", StringRegExpReplace($input, "(?i)([\d+]{4} [a-z]{3} [\d+]{2}.*%)", ""))

Br,

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

That works Uez. Often it easier to take out what you don't want and leave the rest.

@steelsking

I have several ip validator expressions and I just grabbed the first one from the list then added what I needed to that

UEZs method is probably the easiest one providing you are sure the ip address is valid. Mine validates the ip as well as adding the potion after the %

Malkeys should also be good.

This is one of the big benefits of RegEx; flexibility. You can achieve the same results using several methods.

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Link to comment
Share on other sites

hi guys,

sorry, i hit into a problem.

the following line is parsed correctly from this

172.17.112.6 2008 Jan 14 00:02:17 SIN +08:00 %DTP-5-TRUNKPORTON:Port 6/44 has become isl trunk

to

172.17.112.6 DTP-5-TRUNKPORTON:Port 6/44 has become isl trunk

but these lines do not.

172.17.175.129 55182: Jan 14 00:05:38: %LANCE-5-LATECOLL: Unit 0, late collision error

to

172.17.175.129 LANCE-5-LATECOLL: Unit 0, late collision error

kindly assist please. thank you very much!

Edited by steelsking
Link to comment
Share on other sites

Try this.

Local $str = "172.17.153.7 2008 Jan 14 06:57:02 SIN +08:00 %CDP-4-NVLANMISMATCH:Native vlan mismatch detected on port 3/6" & @CRLF & _
            "172.17.175.129 55182: Jan 14 00:05:38: %LANCE-5-LATECOLL: Unit 0, late collision error" & @CRLF & _
            "169.17.154.8 2011 Aug 12 11:00:10 SIN +08:00 %CUP-5-NVLANMISMATCH:Native vlan mismatch detected on port 2/5"

ConsoleWrite(StringRegExpReplace($str, "(?i)(\d{4,}:? [a-z]{3}.*? %)", "") & @LF)

; The part RE pattern, "\d{4,5}:?", matches 4 or more digits, followed by a ":" or not. So now, the start of the captured group will begin with
; either 4 or more digits may be, may be not followed by a ":". That is, in the example the matching groups start with 2008, 55182: and 2011.
;
; Returned:-
; 172.17.153.7 CDP-4-NVLANMISMATCH:Native vlan mismatch detected on port 3/6
; 172.17.175.129 LANCE-5-LATECOLL: Unit 0, late collision error
; 169.17.154.8 CUP-5-NVLANMISMATCH:Native vlan mismatch detected on port 2/5
Link to comment
Share on other sites

thanks for the help Malkey.

but as I went thru the logs file, it failed to parse the following lines correctly.

172.17.183.134 17: 07:45:48: %LINEPROTO-5-UPDOWN: Line protocol on Interface FastEthernet0/13, changed state to down

172.17.174.134 4303: 2y14w: %LINK-3-UPDOWN: Interface FastEthernet0/7, changed state to down

to

172.17.183.134 LINEPROTO-5-UPDOWN: Line protocol on Interface FastEthernet0/13, changed state to down

172.17.174.134 LINK-3-UPDOWN: Interface FastEthernet0/7, changed state to down

please help me. thanks once again!

Link to comment
Share on other sites

Try this one:

$string =   "172.17.153.7 2008 Jan 14 06:57:02 SIN +08:00 %CDP-4-NVLANMISMATCH:Native vlan mismatch detected on port 3/6" & @CRLF & _
                "172.17.183.134 17: 07:45:48: %LINEPROTO-5-UPDOWN: Line protocol on Interface FastEthernet0/13, changed state to down" & @CRLF & _
                "172.17.174.134 4303: 2y14w: %LINK-3-UPDOWN: Interface FastEthernet0/7, changed state to down"
$filter = StringRegExpReplace($string, "(\d+\.\d+\.\d+\.\d+ )(.*%)", "$1")
ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $filter = ' & @CRLF & $filter & @crlf & '>Error code: ' & @error & @crlf) ;### Debug Console
MsgBox(0, "RegEx", $filter)

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

thanks UEZ!

work very well, but just one more little issue.

$string =   "172.17.153.7 2008 Jan 14 06:57:02 SIN +08:00 %CDP-4-NVLANMISMATCH:Native vlan mismatch detected on port 3/6" & @CRLF & _
            "172.17.183.134 17: 07:45:48: %LINEPROTO-5-UPDOWN: Line protocol on Interface FastEthernet0/13, changed state to down" & @CRLF & _
            "172.17.174.134 4303: 2y14w: %LINK-3-UPDOWN: Interface FastEthernet0/7, changed state to down" & @CRLF & _
            "172.17.174.1 13: Cisco Internetwork Operating System Software"

$filter = StringRegExpReplace($string, "(\d+\.\d+\.\d+\.\d+ )(.*%)", "$1")
ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $filter = ' & @CRLF & $filter & @crlf & '>Error code: ' & @error & @crlf) ;### Debug Console
MsgBox(0, "RegEx", $filter)

Thanks!

Link to comment
Share on other sites

And this one?

$string =       "172.17.153.7 2008 Jan 14 06:57:02 SIN +08:00 %CDP-4-NVLANMISMATCH:Native vlan mismatch detected on port 3/6" & @CRLF & _
                    "172.17.183.134 17: 07:45:48: %LINEPROTO-5-UPDOWN: Line protocol on Interface FastEthernet0/13, changed state to down" & @CRLF & _
                    "172.17.174.134 4303: 2y14w: %LINK-3-UPDOWN: Interface FastEthernet0/7, changed state to down" & @CRLF & _
                    "172.17.174.1 13: Cisco Internetwork Operating System Software"
$filter = StringRegExpReplace($string, "(\d+\.\d+\.\d+\.\d+ )(.*%|.*\d+: )", "$1")
ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $filter = ' & @CRLF & $filter & @crlf & '>Error code: ' & @error & @crlf) ;### Debug Console
MsgBox(0, "RegEx", $filter)

Br,

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

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