0

I'm trying to use a regular expression to replace the first character after a single hit, while using PowerShell.

No matter how I try, I can't seem to make it work. Here's what I'm talking about:

Code:

$info = 'AB/F/*ZXCVBN/MTF/ ---'

$regex = [REGEX]'/*'
$regex.Replace($info,"/C",1)

$regex

Output:

/CAB/F/*ZXCVBN/MTF/ ---

I'm simply trying to replace the /F in the expression with /C, but it fails every time.

I'm using /* since I don't really know what character will I find after the first / but that's what I want to replace in the end of the day.

I pretty sure this will be pretty simple but, as you can see, I'm, just not familiar enough with regular expressions.

1
  • have you tried escaping * and use /\* instead? Commented Sep 10, 2014 at 16:25

5 Answers 5

1

Ok, rather than just a comment I guess I'll add an answer. You can use a negative lookbehind to make sure that there are no /'s before what you are matching, so it will only match the first one. Also, as Noah stated the * is not a wildcard, . is. This will match any / plus 1 character that does not have another / anywhere before it in the string:

"(?<!/.*)/."

So in context to your code, it would look like this:

$info = 'AB/F/*ZXCVBN/MTF/ ---'
$regex = [REGEX]"(?<!/)/."
$regex.Replace($info,"/C",1)

Those lines will output:

AB/C/*ZXCVBN/MTF/ ---

Edit: RegEx broken down at RegEx101: http://regex101.com/r/tI7oN1/1

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

1 Comment

Brilliant! Thank you so much! Pretty obvious I have a lot of reading/experimenting to do with regular expressions... Thank you to everyone that pitched in, it is truly appreciated!
0
$info = 'AB/F/*ZXCVBN/MTF/ ---'
$regex = [REGEX]'^([^/]*)/[a-zA-Z]'
$regex.Replace($info,"$1/C",1)
$regex

^([^/]*) - this looks for anything but slashes at the beginning, captured in a group

/[a-zA-Z] - then a slash followed by a letter

The replacement puts back whatever was matched by the first group, and adds /C

Comments

0
$info = 'AB/F/*ZXCVBN/MTF/ ---' 
$regex = [REGEX]'/.'
 $regex.Replace($info,"/C",1)

Or simply:

$info -replace '^(.*?)/.','$1/C'

Comments

0

If you don't want to use the regex, you can accomplish the same thing with -split and -join:

$info = 'AB/F/*ZXCVBN/MTF/ ---'
$info -split '/.',2 -join '/C'

AB/C/*ZXCVBN/MTF/ ---

The ,2 will stop the split after the first match (2 elements). Then re-join the elements with /C.

Comments

0

You are misunderstanding * in regex. It is not a wildcard character.

The star in regex means capture 0 or more of the preceding items in the expression. Wildcard in regex is actually a period.

2 Comments

Nah Noah, don't do that, just use a negative lookbehind: $string -replace "(?<!/.*)/.", "/C"
I tried to think of some ways for a minute to do it in one expression and was drawing blanks. Good idea.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.