Code: Select all
EnableExplicit
Structure ReplaceGr
pos.i
ngr.i
group.s
EndStructure
Procedure RegexReplace2(RgEx, *Result.string, Replace0$, Escaped = 0)
Protected i, CountGr, Pos, Offset = 1
Protected Replace$
Protected NewList item.s()
Protected LenT, *Point
Protected RE2
Protected NewList ReplaceGr.ReplaceGr()
CountGr = CountRegularExpressionGroups(RgEx)
If CountGr > 9
CountGr = 9
EndIf
If ExamineRegularExpression(RgEx, *Result\s)
If Escaped
Replace0$ = ReplaceString(Replace0$, "\r", #CR$)
Replace0$ = ReplaceString(Replace0$, "\n", #LF$)
Replace0$ = ReplaceString(Replace0$, "\t", #TAB$)
Replace0$ = ReplaceString(Replace0$, "\f", #FF$)
EndIf
RE2 = CreateRegularExpression(#PB_Any, "\\\d")
If RE2
If ExamineRegularExpression(RE2, Replace0$)
While NextRegularExpressionMatch(RE2)
If AddElement(ReplaceGr())
ReplaceGr()\pos = RegularExpressionMatchPosition(RE2)
ReplaceGr()\ngr = ValD(Right(RegularExpressionMatchString(RE2), 1))
ReplaceGr()\group = RegularExpressionMatchString(RE2)
EndIf
Wend
EndIf
FreeRegularExpression(RE2)
EndIf
If Not ListSize(ReplaceGr())
*Result\s = ReplaceRegularExpression(RgEx, *Result\s, Replace0$)
ProcedureReturn
EndIf
SortStructuredList(ReplaceGr(), #PB_Sort_Descending, OffsetOf(ReplaceGr\pos), TypeOf(ReplaceGr\pos))
While NextRegularExpressionMatch(RgEx)
Pos = RegularExpressionMatchPosition(RgEx)
Replace$ = Replace0$
ForEach ReplaceGr()
If ReplaceGr()\ngr
Replace$ = ReplaceString(Replace$, ReplaceGr()\group, RegularExpressionGroup(RgEx, ReplaceGr()\ngr), #PB_String_CaseSensitive, ReplaceGr()\pos, 1)
Else
Replace$ = ReplaceString(Replace$, ReplaceGr()\group, RegularExpressionMatchString(RgEx), #PB_String_CaseSensitive, ReplaceGr()\pos, 1)
EndIf
Next
If AddElement(item())
item() = Mid(*Result\s, Offset, Pos - Offset) + Replace$
EndIf
Offset = Pos + RegularExpressionMatchLength(RgEx)
Wend
If AddElement(item())
item() = Mid(*Result\s, Offset)
EndIf
LenT = 0
ForEach item()
LenT + Len(item())
Next
*Result\s = Space(LenT)
*Point = @*Result\s
ForEach item()
CopyMemoryString(item(), @*Point)
Next
FreeList(item())
EndIf
EndProcedure
Procedure.s CompactPath(path$)
Protected idRE, Text.string
Text\s = path$
CompilerSelect #PB_Compiler_OS
CompilerCase #PB_OS_Windows
idRE = CreateRegularExpression(#PB_Any, "^(.{3,11}\\|.{11})(.*)(\\.{6,27}|.{27})$" )
CompilerCase #PB_OS_Linux, #PB_OS_MacOS
idRE = CreateRegularExpression(#PB_Any, "^(.{3,11}/|.{11})(.*)(/.{6,27}|.{27})$" )
CompilerEndSelect
If idRE
RegexReplace2(idRE, @Text, "\1...\3" )
FreeRegularExpression(idRE)
EndIf
ProcedureReturn Text\s
EndProcedure
#StatBar = 0
Define path$
OpenWindow(0, #PB_Ignore, #PB_Ignore, 300, 200, "Test", #PB_Window_SizeGadget | #PB_Window_SystemMenu)
CreateStatusBar(#StatBar, WindowID(0))
AddStatusBarField(100)
AddStatusBarField(#PB_Ignore)
CompilerSelect #PB_Compiler_OS
CompilerCase #PB_OS_Windows
path$ = CompactPath("C:\test\test\test\test\test\test\test\test\test\test\test\test\test\test\test\test\test\test\test\test\test\test\test.txt")
StatusBarText(#StatBar, 1, path$)
MessageRequester("", CompactPath("C:\Users\user\AppData\Roaming\PureBasic\PureBasic.prefs"))
CompilerCase #PB_OS_Linux, #PB_OS_MacOS
MessageRequester("Linux", CompactPath("/home/user/Apps/purebasic/compilers/pbcompilerc"))
CompilerEndSelect
Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow