Jump to content
Chimp

RegExp to trim a part of a string?

Recommended Posts

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

 


small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt....

Share this post


Link to post
Share on other sites

This ?

Func _StringTrimMid2($sInput, $iStartCut, $iEndCut)
  Return StringRegExpReplace ($sInput, "(.{" & $iStartCut-1 & "})(.{" & $iEndCut-$iStartCut+1 & "})(.{" & StringLen($sInput)-$iEndCut-1 & "})","$1$3")
EndFunc

 

Share this post


Link to post
Share on other sites

@Nine I was just about to post a very similar solution, which I translated from here. 😄

Here's what I came up with --

Func _StringTrimMid($sInput, $iStartCut, $iEndCut)

    Local $lFind = "^(.{" & $iStartCut - 1 & "}).{" & ($iEndCut - $iStartCut + 1) & "}"
    Local $lResult = StringRegExpReplace($sInput, $lFind, "\1")

    Return $lResult
EndFunc   ;==>_StringTrimMid

 

Share this post


Link to post
Share on other sites

I'd be tempted to prepend (?s) aka PCRE_DOTALL so that the dot matches newline sequences as well, if ever the case occurs. It all depends on the use case context.


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)

Share this post


Link to post
Share on other sites

Thanks @Nine and @Danp2 :)

31 minutes ago, jchd said:

I'd be tempted to prepend (?s) aka PCRE_DOTALL so that the dot matches newline sequences as well, if ever the case occurs. It all depends on the use case context.

Thanks @jchd

... the use case context should be as much as possible of general use ...
how is your pattern which also allows @cr / @crlf .. (and @tab?)

Thanks


small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt....

Share this post


Link to post
Share on other sites

By default . (dot) matches eveything except newlines. By prepending (?s) to the pattern, subsequent . will match newlines as well. TABs are matched by . in all cases. To be completely generic you may even want to add (*UCP) at the very start of the pattern: this way, extra Unicode newline sequences are matched as well when using (?s).


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)

Share this post


Link to post
Share on other sites
21 minutes ago, jchd said:

By default . (dot) matches eveything except newlines. By prepending (?s) to the pattern, subsequent . will match newlines as well. TABs are matched by . in all cases. To be completely generic you may even want to add (*UCP) at the very start of the pattern: this way, extra Unicode newline sequences are matched as well when using (?s).

.... it would seem that you are right ..... :think:
... I'm kidding of course, ... I had no doubt about that :thumbsup:

Thank You @jchd

Global $sString = "Today " & @CRLF & "I do not feel good"
; chars:           000000     00      011111111112222222
;                  123456     78      901234567890123456

; remove a chunk from char 11 to char 17
MsgBox(0, '', _StringTrimMid($sString, 11, 17))


; Returns the string trimmed by characters from $iStartCut to $iEndCut.
Func _StringTrimMid($sInput, $iStartCut, $iEndCut)

    Local $lFind = "(*UCP)(?s)" & "^(.{" & $iStartCut - 1 & "}).{" & ($iEndCut - $iStartCut + 1) & "}"
    Local $lResult = StringRegExpReplace($sInput, $lFind, "\1")

    Return $lResult

EndFunc   ;==>_StringTrimMid

 

Edited by Chimp

small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt....

Share this post


Link to post
Share on other sites

_StringWordTrimMid ^_^

#include <Array.au3>

Global $sString = "Today " & @CRLF & @CRLF & "I do not feel good i won't quit!"

MsgBox(0, '', _StringWordTrimMid($sString, 2, 6))

; Returns the string trimmed by words from $iStartCut to $iEndCut.
Func _StringWordTrimMid($sInput, $iStartCut, $iEndCut)
    Local $a = StringRegExp($sInput, '[\H]+', 3)
    _ArrayDelete($a, $iStartCut & "-" & $iEndCut)
    Return _ArrayToString($a, " ")
EndFunc

 

Share this post


Link to post
Share on other sites

nice enough, thanks @Deye, but that's another movie..... :)


small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt....

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

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By cruisepandey
      Hi, 
      I have a string like this : 
      Global $Msga = "urrent directory is /send.  (Submission of file with log number 29381077284 is confirmed)";
      I want to extract the number 29381077284  from the string. I did StringSplit to split based on "(" and then use space to reach there, But it's not a good choice. 
      Can anyone help me with regular expression to find the number from String using AutoIT. TIA
    • By Chimp
      Hello
      if I have a string like in the example below,
      is there a regular expression that can surround any "string" (and only strings) within quotes?.
      The whole input string is a "constructor" to populate an array so even if an element contains more words (a phrase) it should be considered as a single word (Elton John should be considered a single word and as that quoted as "Elton John")
      for example
      the following string
      [[Elton John,Peter,Sally,123],[1 one 1,2,3,4 four 4]] should be transformed to this other string
      [["Elton John","Peter","Sally",123],["1 one 1",2,3,"4 four 4"]] Thanks for your help
      Here a small script to use as "guinea pig"
      #include <Array.au3> Local $aArray = [["Elton John", "Peter", "Sally", 123],["one 1", 2, 3, "4 four 4"]] MsgBox(0, "Result", _Array2Json($aArray)) Func _Array2Json($aArray) If (Not IsArray($aArray)) Or (UBound($aArray, 0) > 2) Then Return SetError(1, 0, '') Local $sOpening, $sClosing If UBound($aArray, 0) = 1 Then $sOpening = '[' $sClosing = ']' Else $sOpening = '[[' $sClosing = ']]' EndIf $sOutpt = $sOpening & _ArrayToString($aArray, ",", -1, -1, "],[") & $sClosing ; $sOutpt = ???? how to quote strings ???? Return $sOutpt EndFunc ;==>_Array2Json  
    • By genius257
      Inspired by PHP's preg_split.
      Split string by a regular expression.
      Also supports the same flags as the PHP equivalent.
      v1.0.1
       
      Example:
      #include "StringRegExpSplit.au3" StringRegExpSplit('splitCamelCaseWords', '(?<=\w)(?=[A-Z])') ; ['split', 'Camel', 'Case', 'Words']  
    • 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.
      But 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 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.
      Download Regex Toolkit Regex toolkit.zip  (Sourcode, example and compiled exe file)
      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 
      Regex toolkit.zip  (Sourcode, example and compiled exe file)
    • By lee321987
      Hello, I'm trying to match the second to last line of this:
      foo C:\ foobar foobar x C:\temp\dir Last line with chars Here's my code:
      $test = 'foo' & @CRLF $test &= 'C:\' & @CRLF $test &= 'foobar' & @CRLF $test &= 'hello' & @CRLF $test &= 'C:\temp\dir' & @CRLF $test &= 'Last line with chars' & @CRLF $test &= @CRLF $test &= @CRLF $result = StringRegExp($test, '(?m)^C:\\.*$Last.*') MsgBox(0, '', $result) I'm trying to match line "C:\temp\dir".  Anyone have any ideas?
×
×
  • Create New...