Jump to content

Extract hex number from string


Go to solution Solved by jchd,

Recommended Posts

Hi everyone,

I have this string:

"main_lot      0x111” & @CRLF & “main_version          0xABC” & @CRLF & “main_number 0xDEAD123” & @CRLF & “main_version          0x333"

And I'm trying to extract one specific hexadecimal number, actually main_version from this string by using StringRegExp:
How to get 'ABC' from it?

  • I'm not sure if the original string uses @CRLF, @CR or @LF as a line breaks (received from linux over ssh plink.exe)

I have tried this code but it doesn't work 

#include <Array.au3>

$sLog = "main_lot      0x111” & @CRLF & “main_version          0xABC” & @CRLF & “main_number 0xDEAD123” & @CRLF & “main_version          0x333"
$aVer = StringRegExp($sLog, "main_version\h*(.+)(?:0[xX][[:xdigit:]])", 3)
_ArrayDisplay($aVer)

 

 

Link to post
Share on other sites
  • Solution

Try this patten: (?im)main_version\h*0x([[:xdigit:]]+)

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 post
Share on other sites

Of course, just replace the 3 by 1 as last parameter.

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 post
Share on other sites
  • Moderators

RAMzor,

Quote

I don't think I'll ever learn to use this regular expression language

Well worth the effort to try and understand the basics - it is a phenomenally powerful tool, as jchd has just shown.

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 post
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
  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By TTE26
      How can I get a full name for a folder that starts with "data_" and ends with random numbers and characters?
      I want to get a folders name inside Temp Directory.
      Not sure how to even start. 🤭

      Any suggestions? 🤔
    • By nend
      This is a program that I made to help my self learn better regular expressions.
      There are a lot of other programs/website with the similar functions.
      The main advantage of this program is that you don't have to click a button after every changes.
      The program detected changes and react on it.
      Function:
      Match Match of arrays Match and replace Load source data from website Load source data from a website with GET/POST Load text data from file Clear fields Export and Import settings (you can finish the expression a other time, just export/import it) Cheat sheet DPI aware Generate AutoIt code example code The source code is not difficult and I think most user will understand it.
      In the zip file there is a export files (reg back example), you can drag and drop this files on the gui to import it.
       
      EDIT: Updated to version V1.2.0
      Changes are:
      Expand and collapse of the cheat sheet (Thanks to Melba23 for the Guiextender UDF) Usefull regular expressions websites links included in the program Text data update time EDIT: Updated to version V1.3.0
      Changes are:
       Automatic generate AutoIt code  Icons on the tab  Few minor bug fixes EDIT: Updated to version V1.4.0
      Changes are:
      Link to AutoIt regex helpfile If the regular expression has a error than the text becomes red Option Offset with Match and array of Matches Option Count with Match and replace Some small minor bug fixed EDIT: Updated to version V1.4.1
      Changes are:
      Small bug in "create AutoIt" code fixed EDIT: Updated to version V1.4.2
      Changes are:
      Small bug in "create AutoIt" code fixed Bug with website data  fixed  EDIT: Updated to version V1.4.3
      Changes are:
      DPI aware Click function on cheat sheet to insert function in the regex input field
        (Sourcode, example and compiled exe file) Regex toolkit.zip
    • By FrancescoDiMuro
      Good morning
      I'm playing with SRE and trying to obtain some information from a test file.
      I was testing the pattern on regex101, but when I bring it to AutoIt, it doesn't return the same result as on regex101.
      I am surely (?:missing some important notes about PCRE engine|the pattern is not correct at all).
      Script:
      #include <Array.au3> #include <StringConstants.au3> Test() Func Test() Local $strFileName = @ScriptDir & "\TestFile.txt", _ $strFileContent, _ $arrResult $strFileContent = FileRead($strFileName) If @error Then Return ConsoleWrite("FileRead ERR: " & @error & @CRLF) $arrResult = StringRegExp($strFileContent, '(?sx)User:\h([^\n]+)\n' & _ 'Login\-name:\h([^\n]+)\n' & _ '(?:CaseSensitive:\h([^\n]+)\n)?' & _ 'NTSecurity:\h([^\n]+)\n' & _ '(?:NO\n)?' & _ '(?:Domain:\h([^\n]+)\n)?' & _ 'Timeout:\h([^\n]+)\n' & _ '.*?' & _ 'Member:\h([^\n]+)\n', $STR_REGEXPARRAYGLOBALMATCH) If IsArray($arrResult) Then _ArrayDisplay($arrResult) EndFunc Test file:
      User: AMMINISTRATORE Login-name: ADM CaseSensitive: YES NTSecurity: NO NO Timeout: 00:05:00 Member: AMMINISTRATORI User: Test_User Login-name: Test_User NTSecurity: YES Domain: DNEU Timeout: 00:00:00 Member: OPERATORS Member: OPERATORS Any help (even from cats) it's highly appreciated.

      Cheers
    • By Gianni
      Hi all,
      is there a regular expression pattern that can be used to remove part of a string based on the position of the substring, that is, by specifying the start and end characters of the block to remove? thus obtaining a new string without the "piece" indicated.
      for example:
      Global $sString = "Today I do not feel good" ; chars: 000000000111111111122222 ; 123456789012345678901234 ; remove a chunk from char 9 to char 15 MsgBox(0, '', _StringTrimMid($sString, 9, 15)) ; Returns the string trimmed by characters from $iStartCut to $iEndCut. Func _StringTrimMid($sInput, $iStartCut, $iEndCut) Return StringLeft($sInput, $iStartCut - 1) & StringMid($sInput, $iEndCut + 1) ; can be done the same with a RegExp ?? EndFunc ;==>_StringTrimMid  
    • By Colduction
      Hi again guys!, i had COVID-19 for twice and i couldn't check the forum since 3 or 4 months ago till now! i hope you will get better if you're fighting for beat COVID-19

      I have two question, first is about extracting all of the IP Address from an IP Ranges, for e.g: 192.168.1.1-192.255.255.255 (Start and End are variable and will be defined by the user) and for second one, i have a friend that he is Python programmer, he made a IP Parser that it can support large txt files (1TB) and it can parse all of them under 10min and it also supports low-end PCs that have 1 GB RAM!

      The list that his program parses are:
      #1765497 192.168.1.1 8082 #1765496 192.168.1.1 8084 #1965493 192.168.1.1 8089 #9565495 192.168.1.1 8086 #2565492 192.168.1.1 8081 and it converts very very fast to this:
      192.168.1.1:8082 192.168.1.1:8084 192.168.1.1:8089 192.168.1.1:8086 192.168.1.1:8081 I wonder how to do this via AutoIt, if you can help me in this way, i will be happy✌❤

      Thanks for your helps.

      FIRST_QUESTION_TEST.au3
×
×
  • Create New...