Jump to content

Regex Question


Recommended Posts

I have looked over this regex a bunch of times, but still can't figure out why it is doing this.

#include <Array.au3>
$sLine = "2+-13*3"
$aRegex = StringRegExp($sLine, "(?:-)(-[\d\.]+\*-?[\d\.]+)|(?:\+)(-[\d\.]+\*-?[\d\.]+)|[\d\.]+\*-?[\d\.]+", 1)
_ArrayDisplay($aRegex)

Will return two rows with the first row being blank, but if $sLine = "2-13*3" or $sLine = "2--13*3" it returns the single row. Why is that?

Link to comment
Share on other sites

Will return two rows with the first row being blank

It would return two rows because for capturing the "+"(in "2+-13*3") the engine has to move to the second capturing group and the first group is left blank, but when done with "2--13*3" the match is satisfied with the first group, and hence no second group or row is available.

I find the Regex to be complicated, and it would be a rather easy if you could mention the purpose of the regex.

Regards :)

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.

Link to comment
Share on other sites

Oh so it will return a blank for each group it passes before it finds a match? Then how come it doesn't do that for

#include <Array.au3>
$sLine = "13*3"
$aRegex = StringRegExp($sLine, "(?:-)(-[\d\.]+\*-?[\d\.]+)|(?:\+)(-[\d\.]+\*-?[\d\.]+)|[\d\.]+\*-?[\d\.]+", 1)
_ArrayDisplay($aRegex)

It shouldn't match till the 3rd capture group yet it only returns one row.

The purpose of this regex is I was trying to cleanup the regex I use for the GMP UDF

This instance I was trying to capture the first instance of multiplication integers to include a negative, but it must be +- or -- like 2-4*3 shouldn't capture the negative.

Edited by Onichan
Link to comment
Share on other sites

I would personally provide input, regular expression and expected output. That's what PhoenixXL was hinting at.

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

Link to comment
Share on other sites

Well the input could vary a lot as I was trying to parse math equations. The problem is I have to perform the functions by math order of operations so I was trying to get all multiplication or division out of the input and perform those first from last to right. Which is why I came up with

$aRegex = StringRegExp($sLine, "(?:-)(-[\d\.]+\*-?[\d\.]+)|(?:\+)(-[\d\.]+\*-?[\d\.]+)|[\d\.]+\*-?[\d\.]+|(?:-)(-[\d\.]+/-?[\d\.]+)|(?:\+)(-[\d\.]+/-?[\d\.]+)|[\d\.]+/-?[\d\.]+", 1)

and during my testing it seemed to work. Now I was trying to clean it up a little and simplify it, but I came across this issue. Some inputs I was testing against are

$sLine = "59.94+48+87-23.48*23.78+33*12"
$sLine = "87-23.48*-23.78+46.4"
$sLine = "87--23.48*-23.78+46.4"
$sLine = "87+-23.48*23.78+46.4"

I want just the two numbers that are getting multiplied to include if the number is a negative. So from those 4 examples I want 23.48*23.78, 23.48*-23.78, -23.48*-23.78, -23.48*23.78 respectively in the first and only row. Then I run a 2nd regex to split the two numbers so I can perform the actual math functions which is easy enough.

Now I suppose I could just run the 2nd regex on Ubound($aRegex) - 1 and that in theory should work, but I wanted to know why the regex was doing this just for example 4.

Link to comment
Share on other sites

This will return the operator, and the number

#include <Array.au3>
$sLine1 = "59.94+48+87-23.48*23.78+33*12"
$sLine2 = "87-23.48*-23.78+46.4"
$sLine3 = "87--23.48*-23.78+46.4"
$sLine4 = "87+-23.48*23.78+46.4"
$array = StringRegExp($sLine1, "([-+*]?)(-?\d+\.?\d*)", 4)
For $i = 0 To UBound($array)-1
$aTemp = $array[$i]
_ArrayDisplay($aTemp)
Next
$array = StringRegExp($sLine2, "([-+*]?)(-?\d+\.?\d*)", 4)
For $i = 0 To UBound($array)-1
$aTemp = $array[$i]
_ArrayDisplay($aTemp)
Next
$array = StringRegExp($sLine3, "([-+*]?)(-?\d+\.?\d*)", 4)
For $i = 0 To UBound($array)-1
$aTemp = $array[$i]
_ArrayDisplay($aTemp)
Next
$array = StringRegExp($sLine4, "([-+*]?)(-?\d+\.?\d*)", 4)
For $i = 0 To UBound($array)-1
$aTemp = $array[$i]
_ArrayDisplay($aTemp)
Next
Edited by jdelaney
IEbyXPATH-Grab IE DOM objects by XPATH IEscriptRecord-Makings of an IE script recorder ExcelFromXML-Create Excel docs without excel installed GetAllWindowControls-Output all control data on a given window.
Link to comment
Share on other sites

Onichan,

This returns multiplication expressions

#include <array.au3>
local $sre   = '-?[\.\d]+\*-?[\.\d]+'
local $line1 = '59.94+48+87-23.48*23.78+33*12'
local $Line2 = '87-23.48*-23.78+46.4'
local $Line3 = '87--23.48*-23.78+46.4'
$aResult = stringregexp($line1,$sre,3)
_arraydisplay($aResult)
$aResult = stringregexp($line2,$sre,3)
_arraydisplay($aResult)
$aResult = stringregexp($line3,$sre,3)
_arraydisplay($aResult)

kylomas

edit: corrected code

Edited by kylomas

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Link to comment
Share on other sites

Oh so it will return a blank for each group it passes before it finds a match? Then how come it doesn't do that for

#include <Array.au3>

$sLine = "13*3"

$aRegex = StringRegExp($sLine, "(?:-)(-[d.]+*-?[d.]+)|(?:+)(-[d.]+*-?[d.]+)|[d.]+*-?[d.]+", 1)

_ArrayDisplay($aRegex)

Well in that case no group is captured and the matched part is returned. Therefore only one row is there present. Add a group to the matched part and you would find two blank rows. Hint : when you add a simple regex without any capturing group you still receive a result that is the matched part
#include <Array.au3>
$sLine = "13*3"
$aRegex = StringRegExp($sLine, "(?:-)(-[\d\.]+\*-?[\d\.]+)|(?:\+)(-[\d\.]+\*-?[\d\.]+)|([\d\.]+\*-?[\d\.]+)", 1)
_ArrayDisplay($aRegex)
Regards :) 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.

Link to comment
Share on other sites

Well in that case no group is captured and the matched part is returned. Therefore only one row is there present. Add a group to the matched part and you would find two blank rows. Hint : when you add a simple regex without any capturing group you still receive a result that is the matched part

#include <Array.au3>
$sLine = "13*3"
$aRegex = StringRegExp($sLine, "(?:-)(-[\d\.]+\*-?[\d\.]+)|(?:\+)(-[\d\.]+\*-?[\d\.]+)|([\d\.]+\*-?[\d\.]+)", 1)
_ArrayDisplay($aRegex)
Regards :)

You are right, I didn't try having the last one a group, so then is it best to just grab ubound($aregex)-1 as the result?

There is a reason I need the groups. I can't assume a negative in front of a number is really a negative and not just subtraction. I am using the non captures groups to differentiate between 3+-5*4 and 3-5*4. If I remove the non capturing groups then I have no idea how to tell what's truely negative.

kylomas: That always captures the negative even if the first number it captures isn't truly negative. So it has the issue that I just mentioned that it assumes always negative when it could really be subtracting the first number from the second.

jdelaney: That is just capturing everything in order which really isn't the goal, but thanks.

It seems like just using ubound($aregex)-1 is the best way so that is what I will do. For reference here is the end result of the function:

Func _GMP_ParseFloat($sLine, $iDigits = 128)
    $sLine = StringStripWS($sLine, 8)
    If StringInStr($sLine, "(") Then
        $sParStart = StringInStr($sLine, "(")
        $sParEnd = StringInStr($sLine, ")", 0, -1)
        $sParMid = StringMid($sLine, $sParStart + 1, $sParEnd - $sParStart - 1)
        $sParNew = _GMP_ParseFloat($sParMid, $iDigits)
        If StringRegExp(StringMid($sLine, $sParStart - 1, 1), "\d") Then $sParNew = "*" & $sParNew
        If StringRegExp(StringMid($sLine, $sParEnd + 1, 1), "\d|\.") Then $sParNew &= "*"
        $sLine = StringReplace($sLine, "(" & $sParMid & ")", $sParNew)
    EndIf
    While StringInStr($sLine, "^")
        $aRegex = StringRegExp($sLine, "(?:\-)(-[\d\.]+[\^\-\.\d]+)|(?:\+)(-[\d\.]+[\^\-\.\d]+)|[\d\.]+[\^\-\.\d]+", 1)
        $aRegex2 = StringRegExp($aRegex[0], "(-?[\d\.]+)+", 3)
        $x = UBound($aRegex2) - 3
        $aRes = _GMP_PowFloat($aRegex2[$x + 1], $aRegex2[$x + 2], $iDigits)
        While $x > -1
            $aRes = _GMP_PowFloat($aRegex2[$x], $aRes, $iDigits)
            $x -= 1
        WEnd
        $sLine = StringReplace($sLine, $aRegex[0], $aRes)
    WEnd
    While StringRegExp($sLine, "\*|/")
        $aRegex = StringRegExp($sLine, "(?:-)(-[\d\.]+\*-?[\d\.]+)|(?:\+)(-[\d\.]+\*-?[\d\.]+)|[\d\.]+\*-?[\d\.]+|(?:-)(-[\d\.]+/-?[\d\.]+)|(?:\+)(-[\d\.]+/-?[\d\.]+)|[\d\.]+/-?[\d\.]+", 1)
        $aRegex = $aRegex[UBound($aRegex) - 1]
        If StringInStr($aRegex, "*") Then
            $aRegex2 = StringRegExp($aRegex, "(-?[\d\.]+)\*(-?[\d\.]+)", 1)
            $sLine = StringReplace($sLine, $aRegex, _GMP_MulFloat($aRegex2[0], $aRegex2[1], $iDigits))
        Else
            $aRegex2 = StringRegExp($aRegex, "(-?[\d\.]+)/(-?[\d\.]+)", 1)
            $sLine = StringReplace($sLine, $aRegex, _GMP_DivFloat($aRegex2[0], $aRegex2[1], $iDigits))
        EndIf
    WEnd
    While StringRegExp($sLine, "[\d\.]+\+-?[\d\.]+|[\d\.]+-+[\d\.]+")
        $aRegex = StringRegExp($sLine, "-?[\d\.]+\+-?[\d\.]+|-?[\d\.]+-+[\d\.]+", 1)
        $aRegex = $aRegex[UBound($aRegex) - 1]
        If StringInStr($aRegex, "+") Then
            $aRegex2 = StringRegExp($aRegex, "(-?[\d\.]+)\+(-?[\d\.]+)", 1)
            $sLine = StringReplace($sLine, $aRegex, _GMP_AddFloat($aRegex2[0], $aRegex2[1], $iDigits))
        Else
            $aRegex2 = StringRegExp($aRegex, "(-?[\d\.]+)-(-?[\d\.]+)", 1)
            $sLine = StringReplace($sLine, $aRegex, _GMP_SubFloat($aRegex2[0], $aRegex2[1], $iDigits))
        EndIf
    WEnd
    Return $sLine
EndFunc   ;==>_GMP_ParseFloat

I am not sure how to better explain why I need the regex to perform the way I do.

Link to comment
Share on other sites

... differentiate between 3+-5*4 and 3-5*4 ...

But isn't it true that these two quantities are equal?

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

Is there a reason you are breaking it up?

Execute() will return the value, using order of operations as well.

If you really do need the number*number returned to array, then:

#include <Array.au3>
$sLine1 = "59.94+48+87-23.48*23.78+33*12"
$sLine2 = "87-23.48*-23.78+46.4"
$sLine3 = "87--23.48*-23.78+46.4"
$sLine4 = "87+-23.48*23.78+46.4"

$array = StringRegExp($sLine1, "(-?\d+\.?\d*\*-?\d+\.?\d*)",3)
ConsoleWrite(Execute($sLine1) & @CRLF)
For $i = 0 To UBound($array)-1
 $sMultiply = $array[$i]
 $sValue = Execute($array[$i])
 $sLine1 = StringReplace($sLine1,$sMultiply,$sValue)
Next
ConsoleWrite("string=[" & $sLine1 & "] Value=[" & Execute($sLine1) & "]" & @CRLF)
Edited by jdelaney
IEbyXPATH-Grab IE DOM objects by XPATH IEscriptRecord-Makings of an IE script recorder ExcelFromXML-Create Excel docs without excel installed GetAllWindowControls-Output all control data on a given window.
Link to comment
Share on other sites

But isn't it true that these two quantities are equal?

In this example, yes, but not always. What if $sline = 3-5*-4 and if I didn't account for it then it would flow like this:

$sline = 3-5*-4

$aregex would = -5*-4

then I would do that and get 20

then I replace -5*-4 with 20 and get 320

then the function would see no more arithmetic functions and return 320 which would be incorrect.

Edited by Onichan
Link to comment
Share on other sites

Is there a reason you are breaking it up?

Execute() will return the value, using order of operations as well.

If you really do need the number*number returned to array, then:

#include <Array.au3>
$sLine1 = "59.94+48+87-23.48*23.78+33*12"
$sLine2 = "87-23.48*-23.78+46.4"
$sLine3 = "87--23.48*-23.78+46.4"
$sLine4 = "87+-23.48*23.78+46.4"
$array = StringRegExp($sLine1, "[-+]?(-?\d+\.?\d*\*-?\d+\.?\d*)",3)
ConsoleWrite(Execute($sLine1) & @CRLF)
For $i = 0 To UBound($array)-1
ConsoleWrite($array[$i] & @CRLF)
ConsoleWrite(Execute($array[$i])& @CRLF)
StringReplace($sLine1,$array[$i],Execute($array[$i]))
Next
ConsoleWrite(Execute($sLine1) & @CRLF)

Because execute can't handle arbitrary-precision.
Link to comment
Share on other sites

Onichan,

Does this work as you expect?

#include <Array.au3>
$sLine1 = '59.94+48+87-23.48*23.78+33*12'
$sLine2 = '87-23.48*-23.78+46.4'
$sLine3 = '87--23.48*-23.78+46.4'
$sLine4 = '87+-23.48*23.78+46.4'
$sre = '(?<=[\*\+\\-]|^)[-]?[\d\.]+\*(?<=[\*\+\\-])[-]?[\d\.]+'
$aResults = stringregexp($sLine1,$sre,3)
_arraydisplay($aResults)
$aResults = stringregexp($sLine2,$sre,3)
_arraydisplay($aResults)
$aResults = stringregexp($sLine3,$sre,3)
_arraydisplay($aResults)
$aResults = stringregexp($sLine4,$sre,3)
_arraydisplay($aResults)

kylomas

edit: added positive look ahead assertion to determine whether or not to keep the negation, if it appears.

Edited by kylomas

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Link to comment
Share on other sites

You are right, I didn't try having the last one a group, so then is it best to just grab ubound($aregex)-1 as the result?

Thats not required if all the captures are inside the same capturing group you will get only one row. Check the following.

#include <Array.au3>
$sLine = "87--23.48*-23.78+46.4"
$aRegex = StringRegExp($sLine, "((?:--[\d\.]+\*-?[\d\.]+)|(?:\+-[\d\.]+\*-?[\d\.]+)|(?:[\d\.]+\*-?[\d\.]+))", 1)
_ArrayDisplay($aRegex)

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.

Link to comment
Share on other sites

So you just want to get the resultant number(maybe bigone) through string expressions

^

* /

+ -

In this sequence the regex should be made. Do I get it right ?

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.

Link to comment
Share on other sites

Thats not required if all the captures are inside the same capturing group you will get only one row. Check the following.

#include <Array.au3>
$sLine = "87--23.48*-23.78+46.4"
$aRegex = StringRegExp($sLine, "((?:--[\d\.]+\*-?[\d\.]+)|(?:\+-[\d\.]+\*-?[\d\.]+)|(?:[\d\.]+\*-?[\d\.]+))", 1)
_ArrayDisplay($aRegex)

I guess I could do that, but which way is better? Is it bad to just grab the last row in the array?

So you just want to get the resultant number(maybe bigone) through string expressions

^

* /

+ -

In this sequence the regex should be made. Do I get it right ?

Yes.

Onichan,

Does this work as you expect?

#include <Array.au3>
$sLine1 = '59.94+48+87-23.48*23.78+33*12'
$sLine2 = '87-23.48*-23.78+46.4'
$sLine3 = '87--23.48*-23.78+46.4'
$sLine4 = '87+-23.48*23.78+46.4'
$sre = '(?<=[\*\+\\-]|^)[-]?[\d\.]+\*(?<=[\*\+\\-])[-]?[\d\.]+'
$aResults = stringregexp($sLine1,$sre,3)
_arraydisplay($aResults)
$aResults = stringregexp($sLine2,$sre,3)
_arraydisplay($aResults)
$aResults = stringregexp($sLine3,$sre,3)
_arraydisplay($aResults)
$aResults = stringregexp($sLine4,$sre,3)
_arraydisplay($aResults)

kylomas

edit: added positive look ahead assertion to determine whether or not to keep the negation, if it appears.

Yes that does appear to work, I wasn't familiar with look ahead/behind. Though this still has issues if the string is something like $sLine1 = '87/23.48*23.78'.

I do appreciate everybody's help, but I don't want to keep wasting everyone's time. The current function does work as expected so I will just leave it like that.

Edited by Onichan
Link to comment
Share on other sites

Omnichan, (FYI)

Yes that does appear to work, I wasn't familiar with look ahead/behind. Though this still has issues if the string is something like $sLine1 = '87/23.48*23.78'.

Because I had the division operator backward. This pattern works

(?<=[\*\+\/-]|^)[-]?[\d\.]+\*(?<=[\*\+\/-])[-]?[\d\.]+

I don't want to keep wasting everyone's time

Not at all, interesting little puzzle...

kylomas

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Link to comment
Share on other sites

Omnichan, (FYI)

Because I had the division operator backward. This pattern works

(?<=[\*\+\/-]|^)[-]?[\d\.]+\*(?<=[\*\+\/-])[-]?[\d\.]+

Not at all, interesting little puzzle...

kylomas

If you want to keep working on it then that is fine with me. I think I understand what you did except for the "|^" part, what purpose does that serve?

I was going to modify your regex to equal

$sre = '(?<=[*+/-]|^)[-]?[d.]+[*/](?<=[*+/-])[-]?[d.]+'

That way it included multiply and divide, but then I noticed an issue with doing that and global matches. What if

$sLine1 = '87-23.48*23.78/48/8'

Then the regex would return

[0]|23.48*23.78

[1]|48/8

So it would end up with $sline1 = "558.3544/6" which would equal ~93.059 and would be incorrect. The correct answer would be ~1.454 because of order of operations should say 558.3544/48 then divide by 8.

This is the reason I was doing regex return first match and perform that operation and replace result then loop.

Though your regex is a lot simpler than mine so I could use it like I was using with return first result and loop.

Link to comment
Share on other sites

Onichan,

If you want to keep working on it then that is fine with me.

Chalk it up to stubborness. I am having a bitch of a time learning SRE so this serves two purposes.

"|^" part, what purpose does that serve?

Look for one of the operators or beginning of line

kylomas

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

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