Jump to content
123disconnect

Help me with pattern in StringRegExp

Recommended Posts

123disconnect

I have code

$pattern = 
$str = "%abcd% = Section, hardwareID_1, hardwareID_2, ..... ,HardwareID_n"
$array = StringRegExp( $str, $pattern, 3)

Anyone help me with $pattern value 

I need $array is : 

$array[0] = 'abcd'

$array[1] = 'HardwareID_1'

$array[2] = 'HardwareID_2'

.......

$array[n] = 'HardwareID_n'

No whitespace in all return string . (Sorry for my English)

Thank you verymuch

 

Edited by 123disconnect

Share this post


Link to post
Share on other sites
Xenobiologist

Maybe this way

#include<array.au3>
$pattern = '%(\w+)%|,\s*(\w+)'
$str = "%abcd% = Section, hardwareID_1, hardwareID_2, ..... ,HardwareID_n"
$array = StringRegExp($str, $pattern, 3)
ConsoleWrite(_ArrayToString($array) & @CRLF)

 

  • Like 1

Scripts & functions Organize Includes Let Scite organize the include files

Yahtzee The game "Yahtzee" (Kniffel, DiceLion)

LoginWrapper Secure scripts by adding a query (authentication)

_RunOnlyOnThis UDF Make sure that a script can only be executed on ... (Windows / HD / ...)

Internet-Café Server/Client Application Open CD, Start Browser, Lock remote client, etc.

MultipleFuncsWithOneHotkey Start different funcs by hitting one hotkey different times

Share this post


Link to post
Share on other sites
ViciousXUSMC

Works good Xeno except seems to to produce line extra array elements between the results.

Probably can be fixed in the regex, but I would probably just strip it like this:

#Include <Array.au3>
#Include <StringConstants.au3>

$sPattern = '%(\w+)%|,\s*(\w+)'
$sString = "%abcd% = Section, hardwareID_1, hardwareID_2, ..... ,HardwareID_n"
$aArray = StringRegExp($sString, $sPattern, $STR_REGEXPARRAYGLOBALMATCH)
$sString2 = _ArrayToString($aArray, "|")
$sString2 = StringReplace($sString2, "||", "|")
$aArray2 = StringSplit($sString2, "|", $STR_NOCOUNT)
_ArrayDisplay($aArray2)

 

  • Like 1

Share this post


Link to post
Share on other sites
mikell

Please manage the character set [\w.#] depending on the syntax of the elements to get  :)

#include<array.au3>

$pattern = '(?<=%|,)\s*([\w.#]+)'

$str = "%abcd% = Section, hardwareID_#1, hardwareID_2, ..... ,HardwareID_n"
$array = StringRegExp($str, $pattern, 3)
 _ArrayDisplay($array)

 

Edited by mikell
  • Like 1

Share this post


Link to post
Share on other sites
Xenobiologist

Sorry, I just did a quick answer. I did oversee the ||.
So, mikells solution is more correct.

 

  • Like 1

Scripts & functions Organize Includes Let Scite organize the include files

Yahtzee The game "Yahtzee" (Kniffel, DiceLion)

LoginWrapper Secure scripts by adding a query (authentication)

_RunOnlyOnThis UDF Make sure that a script can only be executed on ... (Windows / HD / ...)

Internet-Café Server/Client Application Open CD, Start Browser, Lock remote client, etc.

MultipleFuncsWithOneHotkey Start different funcs by hitting one hotkey different times

Share this post


Link to post
Share on other sites
ViciousXUSMC

mikell always got the regex :)

I figured a non capture group of some sort would be the fix, mind explaining how come your regex does not produce the same blank elements as Xeno's is it the lookbehind? 

  • Like 1

Share this post


Link to post
Share on other sites
jguinch

Xenobiologist's regex works, but must be enclosed in a duplicate subpattern group :

#include <array.au3>
$pattern = '(?|%(\w+)%|,\s*(\w+))'
$str = "%abcd% = Section, hardwareID_1, hardwareID_2, ..... ,HardwareID_n"
$array = StringRegExp($str, $pattern, 3)

_ArrayDisplay($array)

 

  • Like 2

Share this post


Link to post
Share on other sites
mikell

Haha yes the 'force reset' thing does the trick  :)

The lookaround is not bad either  :P

  • Like 1

Share this post


Link to post
Share on other sites
mikell

Matter of perspective  ;)

  • Like 1

Share this post


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

  • Similar Content

    • Deye
      By Deye
      Hi,
      I want to add any needed conditions to the StringRegExp command so it can pull out only  "File.au3", "WinAPIFiles.au3", "Test.bmp" into the array
      #include <FileConstants.au3> #include <MsgBoxConstants.au3> #include 'WinAPIFiles.au3' #include "File.au3" ; Script Start - Add your code below here Local $bFileInstall = False ; Change to True and ammend the file paths accordingly. ; This will install the file C:\Test.bmp to the script location. If $bFileInstall Then FileInstall("C:\Test.bmp", @ScriptDir & "\Test.bmp") $sFile = FileRead(@ScriptFullPath) $aResults = StringRegExp($sFile, "(?i)(FileInstall\s*|include\s*)(.*)", 3) _ArrayDisplay($aResults) Thanks In Advance
      Deye
    • FroVN
      By FroVN
      i have a text : <Name>Jonh</Name>.<Age>15</Age>
      how i can get Jonh and 15 in one stringregexp? pls give me example
    • therks
      By therks
      I'm looking for a regex genius, cus I'm stumped when it comes to assertions.
      So what I have now, is this regular expression: ([^|=]+)=([^|]+)
      It takes a string (user input) of keys=values separated by pipes (ie: "param=value|param=value") and splits them into an array.
      Example:
      $vParamData = 'example=value|fruit=apple|phrase=Hello world' $aRegEx = StringRegExp($vParamData, '([^|=]+)=([^|]+)', 3) ; Result ; [0] => example ; [1] => value ; [2] => fruit ; [3] => apple ; [4] => phrase ; [5] => Hello world So that's working fine, but I'm wondering if there's also a way I could have this capture escaped pipes instead of splitting by them.
      ie:
      $vParamData = 'pipe test=this \| is a pipe|example=value' $aRegEx = StringRegExp($vParamData, '([^|=]+)=([^|]+)', 3) ; I'm getting this: ; [0] => pipe test ; [1] => this \ ; [2] => example ; [3] => value ; But I'd like a result like this: ; [0] => pipe test ; [1] => this \| is a pipe ; [2] => example ; [3] => value Is there some pattern that would accomplish this, or am I better off parsing it some other way?
    • Robinson1
      By Robinson1
      Well the plan is to use the power of regular expressions engine of AutoIT for patching binary data.
      Something like this: StringRegExp( $BinaryData,  "(?s)\x55\x8B.."
       
      <cut> ... Okay straight to question/problem
      ... certain bytes that are in the range from 0x80 to 0xA0 won't match.
      Hmm seem to be a char encoding problem. In detail these are 27 chars: 0x80, 0x82~8C, 0x8E, 0x91~9C, 0x9E,0x9F
      Here's a small code snippet to explore / explain this problem:
      #include "StringConstants.au3" $TestData = BinaryToString("0x7E7F808182") ;Okay $match = StringRegExp( $TestData ,'\x7E' ,$STR_REGEXPARRAYFULLMATCH) ConsoleWrite('@extended = ' & @extended & ' $match = ' & $match & @CRLF) ;Okay $match = StringRegExp( $TestData ,'\x7F' ,$STR_REGEXPARRAYFULLMATCH) ConsoleWrite('@extended = ' & @extended & ' $match = ' & $match & @CRLF) ;Error no match $match = StringRegExp( $TestData ,'\x80' ,$STR_REGEXPARRAYFULLMATCH) ConsoleWrite('@extended = ' & @extended & ' $match = ' & $match & @CRLF) ;Okay $match = StringRegExp( $TestData ,'\x81' ,$STR_REGEXPARRAYFULLMATCH) ConsoleWrite('@extended = ' & @extended & ' $match = ' & $match & @CRLF) ;Error no match $match = StringRegExp( $TestData ,'\x82' ,$STR_REGEXPARRAYFULLMATCH) ConsoleWrite('@extended = ' & @extended & ' $match = ' & $match & @CRLF) ;~ output: ;~ @extended = 2 $match = ;~ @extended = 3 $match = ;~ @extended = 0 $match = 1 ;~ @extended = 5 $match = ;~ @extended = 0 $match = 1 Hmm what to do? Go back and use the 'numberstring monster' implementation or just omit that range of 'unsafe bytes'. What is the root of this problem?
      Any idea how to fix this?
       
      Update: Okay I know a byte is not a character.
      But StringRegExp operates on String and so character level.
      Okay as long as you stay at Ansi encoding and only use /x00 - /X7F in the search pattern using  StringRegExp works well to search for binary data.
      What bytes can be matched that are in the range from /X7F - /xFF is also depending on the code page.
      So this avoid to search for bytes in the range from 0x80-0xa0 only applies to Germany.
      I just change this country setting:

      to Thai and now near all bytes from /X7F - /xFF fails to match.
    • Carm01
      By Carm01
      Hello,
      I have spent the past day fooling with StringRegExp to no avail attempting to get what would be a simple solution to an issue using StringRegExp.
      I will post the code in a sec. The string 'Java x Update y' where x and y are numeric values ONLY if a letter is mixed in anywhere then it should fail. I have been able to successfully deal with the x value so if x = 1234 or a1234 or 1a234 or 1234a would result in a fail if 'a' was in the string. However, when y = 1a234 then I get an output of 1 and when y = 1234a then the output = 1234 when both should fail. I am probably overlooking something simple and in looking through all the material and experimenting I am unable to figure it out and my experience with stringregexp and trying to find examples of this proved difficult. If someone could assist or point me to a thread ? Here is my code ; prob a simple fix. I am also trying to avoid white spaces.
      Thanks in advance
      #include <array.au3> $aArray = StringRegExp('Java 3009 Update 1a21', '(?i)Java (\d+) Update (\d+)', $STR_REGEXPARRAYGLOBALMATCH) If @error Then Exit _ArrayDisplay($aArray)  
×