Jump to content
Sign in to follow this  
michaelslamet

[SOLVED] Need RegEx Help

Recommended Posts

michaelslamet

Hi,

I have text like this:

OK

+AKJA: 4

+LAKLSK: 2,"blablablabla:098912983912 blblas blasab lblablablsa *334*no okoakdo#, kjadla : *229*023456721212#",0

Those 3 lines are in 1 variable.

From above example, I want a RegEx that produce "098912983912"

The valid number can be 9~13 long AND always start with 0

If the number start with 62 then replace the 62 with 0

If found more than 1 result (like example above, it is 098912983912 and 023456721212, then the valid one is the one that not start with *)

Seems very complicated for me :(

Edited by michaelslamet

Share this post


Link to post
Share on other sites
Mat

Well, not all of that is possible with regex alone.

First step is get the pattern for 9-13 digits starting with either a 0 or 62: (62d{7,11}|0d{8,12})

Things you need to know: | is a group () matches either the left side OR the right side. {x,y} matches the previous match repeated between x and y times. d matches any digit (0-9)

Next is to get the asterisk before (if it exists). Now this is where the easiest way is two do two regexs. The first to match it without a *, then next to match it with. We do this by using [^*], which matches the class of anything other than a *. We have to escape the asterisk (so its *) as its also a special character in regex.

So my final script looks something like:

#include <Constants.au3>

Local $sInput = "OK" & @CRLF & @CRLF & _
        "+AKJA: 4" & @CRLF & @CRLF & _
        "+LAKLSK: 2,""blablablabla:098912983912 blblas blasab lblablablsa *334*no okoakdo#, kjadla : *229*023456721212#"",0"

Local $aRes = StringRegExp($sInput, "[^\*](62\d{7,11}|0\d{8,12})", 3)

If @error Then
    $aRes = StringRegExp($sInput, "(62\d{7,11}|0\d{8,12})", 3)

    If @error Then
        ConsoleWrite("No matches!" & @LF)
        Exit 1
    EndIf
EndIf

MsgBox($MB_SYSTEMMODAL, Default, "Match found: " & $aRes[0])

You will have to replace the 62 with 0 in code. I don't think there is any way to do that with regex.

I hope you can understand the regex. They are complex, but very powerful. There is probably a way to do the above in a single regular expression, but I'm not nearly good enough for that.

Share this post


Link to post
Share on other sites
PhoenixXL

You can use lookarounds to check for asterisk

Here is an Example

#include <Array.au3>
;Keypoint : Return the 9-13 digits Number which is not preceded by a star and if having '62' as the starting replace it with a '0'


$String = "OK" & @CRLF & _
"+AKJA: 4" & @CRLF & _
'+LAKLSK: 2,"blablablabla:098912983912 blblas blasab 621234567890 *0626897532158 lblablablsa *334*no okoakdo#, kjadla : *229*023456721212#",0'

;First lets replace the 62 with a 0 if possible
$String = StringRegExpReplace( $String, '(?<!\*)(62)(\d{7,11})', '0\2' )

;~ ConsoleWrite( $String & @CR )

Local $aRet = StringRegExp( $String, '(?<!\*)(0\d{8,12})', 3 )

_ArrayDisplay( $aRet )
Edited by PhoenixXL

My code:

PredictText: Predict Text of an Edit Control Like Scite. Remote Gmail: Execute your Scripts through Gmail. StringRegExp:Share and learn RegExp.

Run As System: A command line wrapper around PSEXEC.exe to execute your apps scripts as System (LSA). Database: An easier approach for _SQ_LITE beginners.

MathsEx: A UDF for Fractions and LCM, GCF/HCF. FloatingText: An UDF for make your text floating. Clipboard Extendor: A clipboard monitoring tool. 

Custom ScrollBar: Scroll Bar made with GDI+, user can use bitmaps instead. RestrictEdit_SRE: Restrict text in an Edit Control through a Regular Expression.

Share this post


Link to post
Share on other sites
PhoenixXL

@cheapbydre,

are you advertising here..!

Edit: maybe the cheapbydre deleted his posted

Edited by PhoenixXL

My code:

PredictText: Predict Text of an Edit Control Like Scite. Remote Gmail: Execute your Scripts through Gmail. StringRegExp:Share and learn RegExp.

Run As System: A command line wrapper around PSEXEC.exe to execute your apps scripts as System (LSA). Database: An easier approach for _SQ_LITE beginners.

MathsEx: A UDF for Fractions and LCM, GCF/HCF. FloatingText: An UDF for make your text floating. Clipboard Extendor: A clipboard monitoring tool. 

Custom ScrollBar: Scroll Bar made with GDI+, user can use bitmaps instead. RestrictEdit_SRE: Restrict text in an Edit Control through a Regular Expression.

Share this post


Link to post
Share on other sites
michaelslamet

Well, not all of that is possible with regex alone.

First step is get the pattern for 9-13 digits starting with either a 0 or 62: (62d{7,11}|0d{8,12})

Things you need to know: | is a group () matches either the left side OR the right side. {x,y} matches the previous match repeated between x and y times. d matches any digit (0-9)

Next is to get the asterisk before (if it exists). Now this is where the easiest way is two do two regexs. The first to match it without a *, then next to match it with. We do this by using [^*], which matches the class of anything other than a *. We have to escape the asterisk (so its *) as its also a special character in regex.

So my final script looks something like:

#include <Constants.au3>

Local $sInput = "OK" & @CRLF & @CRLF & _
        "+AKJA: 4" & @CRLF & @CRLF & _
        "+LAKLSK: 2,""blablablabla:098912983912 blblas blasab lblablablsa *334*no okoakdo#, kjadla : *229*023456721212#"",0"

Local $aRes = StringRegExp($sInput, "[^\*](62\d{7,11}|0\d{8,12})", 3)

If @error Then
    $aRes = StringRegExp($sInput, "(62\d{7,11}|0\d{8,12})", 3)

    If @error Then
        ConsoleWrite("No matches!" & @LF)
        Exit 1
    EndIf
EndIf

MsgBox($MB_SYSTEMMODAL, Default, "Match found: " & $aRes[0])

You will have to replace the 62 with 0 in code. I don't think there is any way to do that with regex.

I hope you can understand the regex. They are complex, but very powerful. There is probably a way to do the above in a single regular expression, but I'm not nearly good enough for that.

Hi Mat, thanks a lot for your help and explanation :)

It's working great except that when it found some series number, if the length is not 9~13 then it the number should not valid.

For example, this string:

OK

+AKJA: 4

+LAKLSK: 2,"blablablabla:0989129839120000000 blblas blasab lblablablsa *334*no okoakdo#, kjadla : *229*023456721212#",0

Should return "023456721212" because the length of the first series number (0989129839120000000) is not between 9~13, so we will take second series number (023456721212) even if it start with *

The length of those numbers should be after replace "62" with "0"

Edited by michaelslamet

Share this post


Link to post
Share on other sites
michaelslamet

You can use lookarounds to check for asterisk

Here is an Example

#include <Array.au3>
;Keypoint : Return the 9-13 digits Number which is not preceded by a star and if having '62' as the starting replace it with a '0'


$String = "OK" & @CRLF & _
"+AKJA: 4" & @CRLF & _
'+LAKLSK: 2,"blablablabla:098912983912 blblas blasab 621234567890 *0626897532158 lblablablsa *334*no okoakdo#, kjadla : *229*023456721212#",0'

;First lets replace the 62 with a 0 if possible
$String = StringRegExpReplace( $String, '(?<!\*)(62)(\d{7,11})', '0\2' )

;~ ConsoleWrite( $String & @CR )

Local $aRet = StringRegExp( $String, '(?<!\*)(0\d{8,12})', 3 )

_ArrayDisplay( $aRet )

The master of RegEx is here :)

"62" should be replaced with "0" only if the "62" is found in the first and second character in that series of number.

Also, using this string:

OK

+AKJA: 4

+LAKLSK: 2,"blablablabla:0989129839120000000 blblas blasab lblablablsa *334*no okoakdo#, kjadla : *229*023456721212#",0

Should return "023456721212" because the length of the first series number (0989129839120000000) is not between 9~13, so we will take second series number (023456721212) even if it start with *

The length of those numbers should be after replace "62" with "0"

Thanks, PhoenixXL!

Share this post


Link to post
Share on other sites
PhoenixXL

its better to specify the conditions beforehand

Now this is what I get

Priorities

  • The digits should be 9-13 {if 62 is there in first index or second its len is considered as 1[coz it will be replaced by a "0"] }

  • Now find the digits that don't precede a asterisk, if such a condition is not found then check for the digits with an asterisk even
Now I have some questions
  • You have two conflicting cases that is - the range of digits should be 9 - 13 but you are asking to match the digits present here "*229*023456721212#", So do you mean to match such cases where there is a non-digit bw the digits or a horizontal space
  • Plus do you want to return a global match or just a single match
  • Im still confused are you regarding your 9-13 numbers range including "62" and "0" or not
Edited by PhoenixXL

My code:

PredictText: Predict Text of an Edit Control Like Scite. Remote Gmail: Execute your Scripts through Gmail. StringRegExp:Share and learn RegExp.

Run As System: A command line wrapper around PSEXEC.exe to execute your apps scripts as System (LSA). Database: An easier approach for _SQ_LITE beginners.

MathsEx: A UDF for Fractions and LCM, GCF/HCF. FloatingText: An UDF for make your text floating. Clipboard Extendor: A clipboard monitoring tool. 

Custom ScrollBar: Scroll Bar made with GDI+, user can use bitmaps instead. RestrictEdit_SRE: Restrict text in an Edit Control through a Regular Expression.

Share this post


Link to post
Share on other sites
guinness

The master of RegEx is here

Their words not mine.

UDF List:

 
_AdapterConnections()_AlwaysRun()_AppMon()_AppMonEx()_ArrayFilter/_ArrayReduce_BinaryBin()_CheckMsgBox()_CmdLineRaw()_ContextMenu()_ConvertLHWebColor()/_ConvertSHWebColor()_DesktopDimensions()_DisplayPassword()_DotNet_Load()/_DotNet_Unload()_Fibonacci()_FileCompare()_FileCompareContents()_FileNameByHandle()_FilePrefix/SRE()_FindInFile()_GetBackgroundColor()/_SetBackgroundColor()_GetConrolID()_GetCtrlClass()_GetDirectoryFormat()_GetDriveMediaType()_GetFilename()/_GetFilenameExt()_GetHardwareID()_GetIP()_GetIP_Country()_GetOSLanguage()_GetSavedSource()_GetStringSize()_GetSystemPaths()_GetURLImage()_GIFImage()_GoogleWeather()_GUICtrlCreateGroup()_GUICtrlListBox_CreateArray()_GUICtrlListView_CreateArray()_GUICtrlListView_SaveCSV()_GUICtrlListView_SaveHTML()_GUICtrlListView_SaveTxt()_GUICtrlListView_SaveXML()_GUICtrlMenu_Recent()_GUICtrlMenu_SetItemImage()_GUICtrlTreeView_CreateArray()_GUIDisable()_GUIImageList_SetIconFromHandle()_GUIRegisterMsg()_GUISetIcon()_Icon_Clear()/_Icon_Set()_IdleTime()_InetGet()_InetGetGUI()_InetGetProgress()_IPDetails()_IsFileOlder()_IsGUID()_IsHex()_IsPalindrome()_IsRegKey()_IsStringRegExp()_IsSystemDrive()_IsUPX()_IsValidType()_IsWebColor()_Language()_Log()_MicrosoftInternetConnectivity()_MSDNDataType()_PathFull/GetRelative/Split()_PathSplitEx()_PrintFromArray()_ProgressSetMarquee()_ReDim()_RockPaperScissors()/_RockPaperScissorsLizardSpock()_ScrollingCredits_SelfDelete()_SelfRename()_SelfUpdate()_SendTo()_ShellAll()_ShellFile()_ShellFolder()_SingletonHWID()_SingletonPID()_Startup()_StringCompact()_StringIsValid()_StringRegExpMetaCharacters()_StringReplaceWholeWord()_StringStripChars()_Temperature()_TrialPeriod()_UKToUSDate()/_USToUKDate()_WinAPI_Create_CTL_CODE()_WinAPI_CreateGUID()_WMIDateStringToDate()/_DateToWMIDateString()Au3 script parsingAutoIt SearchAutoIt3 PortableAutoIt3WrapperToPragmaAutoItWinGetTitle()/AutoItWinSetTitle()CodingDirToHTML5FileInstallrFileReadLastChars()GeoIP databaseGUI - Only Close ButtonGUI ExamplesGUICtrlDeleteImage()GUICtrlGetBkColor()GUICtrlGetStyle()GUIEventsGUIGetBkColor()Int_Parse() & Int_TryParse()IsISBN()LockFile()Mapping CtrlIDsOOP in AutoItParseHeadersToSciTE()PasswordValidPasteBinPosts Per DayPreExpandProtect GlobalsQueue()Resource UpdateResourcesExSciTE JumpSettings INISHELLHOOKShunting-YardSignature CreatorStack()Stopwatch()StringAddLF()/StringStripLF()StringEOLToCRLF()VSCROLLWM_COPYDATAMore Examples...

Updated: 22/04/2018

Share this post


Link to post
Share on other sites
PhoenixXL

Till the information

#include <Array.au3>

;Im assuming that 62 and 0 is included in the count of the integers
#cs
098989898 : Match
6231231234 : Match after replacing 62 to 0 the number is still 9 digits
0623123123 : Match after replacing 062 with 00 the number is still 9 digits
1623123123 : No Match because 62 being in second index will get replaced but the starting is 1 hence the number is not captured
*023456721212 : Match when no other match is found
#ce

Local $sString = "blablablabla:0989129839120000000 blblas blasab lblablablsa *334*no okoakdo#, kjadla : *229*023456721212#"

_Main( $sString )

Func _Main($String)
;Find 62 in First Index, when found replace with a 0
$String = StringRegExpReplace($String, "(\D|^)(62)(\d{7,11})(\D|$)", '${1}0\3\4')

;Find 062 in Second Index, when found replace with a 0
$String = StringRegExpReplace($String, "(\D|^)(062)(\d{6,10})(\D|$)", '${1}00\3\4')

ConsoleWrite($String & @CR)

Local $aRet = StringRegExp($String, "(?:\D|^)(?<!\*)(0\d{8,12})(?:\D|$)", 3)
If @error Then $aRet = StringRegExp($String, "(?:\D|^)(0\d{8,12})(?:\D|$)", 3)

_ArrayDisplay($aRet)

EndFunc   ;==>_Main

If this doesnt fulfill the conditions please do answer my questions..!

Regards

Phoenix XL

  • Like 1

My code:

PredictText: Predict Text of an Edit Control Like Scite. Remote Gmail: Execute your Scripts through Gmail. StringRegExp:Share and learn RegExp.

Run As System: A command line wrapper around PSEXEC.exe to execute your apps scripts as System (LSA). Database: An easier approach for _SQ_LITE beginners.

MathsEx: A UDF for Fractions and LCM, GCF/HCF. FloatingText: An UDF for make your text floating. Clipboard Extendor: A clipboard monitoring tool. 

Custom ScrollBar: Scroll Bar made with GDI+, user can use bitmaps instead. RestrictEdit_SRE: Restrict text in an Edit Control through a Regular Expression.

Share this post


Link to post
Share on other sites
michaelslamet

PhoenixXL, thanks a lot! :-)

I'm sorry that I'm not follow up my own thread for many days for some reasons :(

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
Sign in to follow this  

×