0

Good afternoon. I am a VB.Net programmer that has made every attempt to implement regular expressions into my applications as much as possible. I choose regex over Net methods because practice makes perfect. This is only for my regex knowledge benefit.

Take a string like this for example ignoring the quotes. ":1Af404080A83hfndsgt4u47", the part of the string i am looking at is these 8 values. "04080A83" These can change. The values are not important but the position. Starting from 0 the first char position will be 5 to 12. I know we can match any char until {5} but it possible to replace a range. Example {5,12}. Final output would be "":1Af4Hello123hfndsgt4u47"" Hello123

Thank you for your time. It might not be possible like i said for my own Benita.

2
  • try posting some of your attempts...you never know you may have been close. Commented Aug 22, 2013 at 12:23
  • 1
    I hope you understand that there are much, MUCH simpler ways than using regex. Commented Aug 22, 2013 at 12:34

4 Answers 4

1

You can use a lookbehind to make sure that you skip the first five characters (?<=^.{5}), and then you can simply match 8 characters with .{8}, which you can replace with whatever you like.

result = Regex.Replace(input, "(?<=^.{5}).{8}", "Hello123")

Working demo.

Sign up to request clarification or add additional context in comments.

2 Comments

Perfect thank you very much. am i right this is a positive lookbehind? never quite understand the meaning lol
@user2509066 yes it is. have a read at the link in the answer, it explains the matter really well. if this solved your problem, please consider accepting the answer by ticking the check mark next to it. this will show future visitors that (and how) your problem has been solved, and give me some credit (and a little to you as well).
1

Why do you need regex to replace a range in a string? String methods are more efficient and often more readable.

string input = ":1Af404080A83hfndsgt4u47";
string replace = "Hello123";
int start = 5;
string result = "";
if (input.Length >= start)
{
    result = input.Substring(0, start) + replace + input.Substring(start + replace.Length);
    Console.Write(result);
}

Demo

VB.NET:

Dim input As String = ":1Af404080A83hfndsgt4u47"
Dim replace As String = "Hello123"
Dim start As Integer = 5
Dim result As String = ""
If input.Length >= start Then
    result = input.Substring(0, start) & replace & input.Substring(start + replace.Length)
    Console.Write(result)
End If

3 Comments

Well, he said he uses regex to learn regex. This is apparently not going to be production code.
@m.buettner: I have overlooked that.
I'v programmed in Visual Basics for over a decade. I'm very much aware how to do this by substrings. As i said in my original post i am using regex when ever i can even if it makes no sense to. It's all about my personal learning. But thanks any way
0

A way to do this with a regex would be the following:

Dim regex As Regex = new Regex("(.{5}).{8}(.*)")
Console.WriteLine(regex.Replace(":1Af404080A83hfndsgt4u47", "$1Hello123$2"))

See also: How to use named groups when performing a Regex.Replace() How to RegEx Replace named groups

Comments

0

I am not exactly sure what you mean by

I know we can match any char until {5} but it possible to replace a range. Example {5,12}.

but this Regex should suffice for your needs.

(?<=^(\w{4}))\w{8}

1 Comment

The ^ needs to go inside the lookbehind. Otherwise this is bound to fail (as it's requiring four characters to the left of the beginning of the string).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.