Jump to content

Trying to remove character at specific position with StringRegExp


Recommended Posts

Hi!

I'm trying to remove a character at a specific position (somewhere in the string) with StringRegExp.

Here's my current "workaround":

For $i In StringRegExp(StringReplace("1234567890", 3, "X"), "[^X]", 3)
    ConsoleWrite("___" & $i & @CRLF)
Next

Is there a way to do this with StringRegExp only?

Greets,

-supersonic.

Edited by supersonic
Link to comment
Share on other sites

Hi!

I'm trying to remove a character at a specific position (somewhere in the string) with StringRegExp.

Here's my current "workaround":

For $i In StringRegExp(StringReplace("1234567890", 3, "X"), "[^X]", 3)
    ConsoleWrite("___" & $i & @CRLF)
Next

Is there a way to do this with StringRegExp only?

Greets,

-supersonic.

If you only want to remove a particular character couldn't you just use

$newstring = StringReplace("1234567890", 3, "")

Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.
Link to comment
Share on other sites

I would like to remove a character by position. I don't know what character it is. I know only its position and its position could change.

Furthermore the result should be returned as an array for loop processing.

My example works well - but I would like to avoid using StringReplace.

Edited by supersonic
Link to comment
Share on other sites

Here are a couple of ways.

Local $str = "1234567890"
Local $CharPos = 3

For $i In StringSplit(StringReplace($str, StringMid($str, $CharPos, 1), ""), "", 2)
    ConsoleWrite("___" & $i & @CRLF)
Next

ConsoleWrite(@LF)
;or
For $i In StringSplit(StringLeft($str, $CharPos - 1) & StringRight($str, StringLen($str) - $CharPos), "", 2)
    ConsoleWrite("___" & $i & @CRLF)
Next

ConsoleWrite(@LF)
;or
For $i In StringRegExp(StringLeft($str, $CharPos - 1) & StringRight($str, StringLen($str) - $CharPos), "(.)", 3)
    ConsoleWrite("___" & $i & @CRLF)
Next

Edit:: Added third method.

Edited by Malkey
Link to comment
Share on other sites

What about this version?

#include <Array.au3>
$string = "abcdefg1hijklm"
$rem_pos = 8
$array = StringSplit(StringLeft($string, $rem_pos - 1) & StringRight($string, StringLen($string) - $rem_pos), "", 2)
_ArrayDisplay($array)

Edit: ok, it is similar to Malkey's 3rd example. :mellow:

Br,

UEZ

Edited by UEZ

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Link to comment
Share on other sites

@PowerCat:

You are right!

Everything could change - The character and its position and the string length itself.

The only constant I have is the position of the character.

I hope there is a search pattern to remove character at a specific position...

Link to comment
Share on other sites

That's very nebulous and unclear.

You claim both that its position "could change" and then claim that you "only know its position"

Can it change or not?? I'm confused. How do you change something which you don't know what it is, at a position which you don't know either? How do you know what to change?

You can simply use StringLeft\Right to grab what's before and after

Like if it's always the third char in a string of 10 char,

$str = "1234567890"

$str1 = StringLeft($str, "2") & StringRight($str, "7")

ConsoleWrite($str1)
Edited by PowerCat
Link to comment
Share on other sites

Is this what you are looking for?

$s_String = "This is a test string."
$s_Char = "r"
$a_SRE = StringRegExp($s_String, "(?i)^(.+?)(" & $s_Char & ")(.*)$", 3);; Everything preceeding $s_Char is in [0] $s_Char is in [1] and the balance is in [2]

If you want it to be case sensitive remove the "(?i)"

Edited by GEOSoft

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

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