Sign in to follow this  
Followers 0
supersonic

Trying to remove character at specific position with StringRegExp

12 posts in this topic

#1 ·  Posted (edited)

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

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

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

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

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

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

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!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Share this post


Link to post
Share on other sites

Thank you! :mellow: - I have already thought about your examples.

I was looking for solving this with StringRegExp only to make it as short as possible.

Is there really no regular expression to point to a single character position?

Share this post


Link to post
Share on other sites

#8 ·  Posted (edited)

Wait, you'd like to remove a character which changes, at a position which changes?

Are there any constants in your strings?

Edited by PowerCat

Share this post


Link to post
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...

Share this post


Link to post
Share on other sites

#11 ·  Posted (edited)

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

Share this post


Link to post
Share on other sites

#12 ·  Posted (edited)

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!"

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  
Followers 0