Replace the last occurrence of a character in a string

I have the following line:

01/01/2014 blbalbalbalba blabla / blabla

I would like to replace the last slash with a space and keep the first two slashes in the date.

The closest thing I came up with was this:

PS E:\> [regex] $regex = '[a-z]'
PS E:\> $regex.Replace('abcde', 'X', 3)
XXXde

but I don’t know how to start from the end of the line. Any help would be greatly appreciated.

Editing my question to clarify: I just want to replace the last slash character with a space, so:

01/01/2014 blbalbalbalba blabla / blabla becomes 01/01/2014 blbalbalbalba blabla blabla

Knowing that the length of "blabla" varies from one line to another, and the slash character can be anywhere.

Thank:)

+4
5

:

(.*)[/](.*)

:

$1 $2

:

(.*) , ( ). , , placeholder $, ( , , $1 ), replace, , , . 01/01/2014 blbalbalbalba blabla

[/] .

(.*) , , . blabla, $2 placeholder.

, , , , . " " (.. ), , . , , , , [/] .

+10

lookahead:

'01/01/2014 blbalbalbalba blabla/blabla' -replace '/(?=[^/]+$)',' '

01/01/2014 blbalbalbalba blabla blabla

'/(? = [^/] + $)' '/', 'not/' EOL, , , , .

+1
'01/01/2014 blbalbalbalba blabla/blabla' -replace '^(\d{2}/\d{2})/(\d{4} .*)','$1 $2'
# outputs this:
# 01/01 2014 blbalbalbalba blabla/blabla
0
source

Here you can do it without regular expressions:

$string = "01/01/2014 blbalbalbalba blabla/blabla"
$last_index = $string.LastIndexOf('/')
$chars = $string.ToCharArray()
$chars[$last_index] = ' '
$new_string = $chars -join ''

Another way:

$string = "01/01/2014 blbalbalbalba blabla/blabla"
$last_index = $string.LastIndexOf('/')
$new_string = $string.Remove($last_index, 1).Insert($last_index, ' ')
0
source

$ is the anchor for the end of the line. So (. *?) ([A-d]) $

should match what you want, and the thing in () is what you want to replace.

Regards

-1
source

Source: https://habr.com/ru/post/1547192/


All Articles