Questions tagged [last-occurrence]

The last occurrence is the first occurrence starting backwards. Used for questions related to finding the last occurrence of specified data in a data set, matching the last occurrence of a pattern match from a string, or referencing the previously found occurrences.

The last occurrence is the first occurrence starting backwards. Used for questions related to finding the last occurrence of specified data in a data set, matching the last occurrence of a pattern match from a string, or referencing the previously found occurrences.

46 questions
155
votes
15 answers

Replace last occurrence of a string in a string

Anyone know of a very fast way to replace the last occurrence of a string with another string in a string? Note, the last occurrence of the string might not be the last characters in the string. Example: $search = 'The'; $replace = 'A'; $subject =…
Kirk Ouimet
  • 23,368
  • 41
  • 108
  • 164
87
votes
14 answers

How to find the last occurrence of an item in a Python list

Say I have this list: li = ["a", "b", "a", "c", "x", "d", "a", "6"] As far as help showed me, there is not a builtin function that returns the last occurrence of a string (like the reverse of index). So basically, how can I find the last occurrence…
Shaokan
  • 6,522
  • 12
  • 52
  • 78
25
votes
4 answers

Replace Last Occurrence of a character in a string

I am having a string like this "Position, fix, dial" I want to replace the last double quote(") with escape double quote(\") The result of the string is to be "Position, fix, dial\" How can I do this. I am aware of replacing the first occurrence…
Mahe
  • 2,647
  • 13
  • 47
  • 68
19
votes
13 answers

How to explode only on the last occurring delimiter?

$split_point = ' - '; $string = 'this is my - string - and more'; How can I make a split using the second instance of $split_point and not the first one. Can I specify somehow a right to left search? Basically how do I explode from right to left. I…
Codex73
  • 5,445
  • 9
  • 53
  • 75
17
votes
5 answers

SED: multiple patterns on the same line, how to match/parse first one

I have a file, which holds phone number data, and also some useless stuff. I'm trying to parse the numbers out, and when there is only 1 phone number / line, it's not problem. But when I have multiple numbers, sed matches the last one (even though…
julumme
  • 2,136
  • 2
  • 20
  • 33
15
votes
1 answer

tsql last "occurrence of" inside a string

I have got field containing comma separated values. I need to extract the last element in the list. I have tried with this: select list_field, LTRIM(RTRIM(right(list_field, len(list_field) - CHARINDEX(',',list_field)))) But it returns the last part…
Alberto De Caro
  • 4,849
  • 9
  • 42
  • 72
7
votes
8 answers

finding the last occurrence of an item in a list python

I wish to find the last occurrence of an item 'x' in sequence 's', or to return None if there is none and the position of the first item is equal to 0 This is what I currently have: def PositionLast (x,s): count = len(s)+1 for i in s: …
efc1
  • 71
  • 1
  • 5
5
votes
1 answer

Replace last occurrence of at least double new line (\n\n) in string PHP

I need to replace last occurrence of at least double new line (\n\n) in a string, so it should be \n\n or \n\n\n or \n\n\n\n an so on (at least 2 \n) by the "@@". I think that it should be preg_replace. I have tried many option that is in the…
IncreMan
  • 326
  • 2
  • 10
5
votes
2 answers

Matlab: Find row indice of first occurrence for each column of matrix (without using loops)

For each column of a matrix A consisting of '0' and '1', I would like to find the column indices of the first occurrence of '1' if exists. For example, if A is defined as: A=[0 0 0 0; 0 0 0 1; 0 0 0 0; 0 0 0 1; 1 0 0 0; 0 1 0 1; 1 1 0…
5
votes
4 answers

Bash: how to use sed to replace only the last occurence in a file?

Having a file containing repeated commented lines like: # ScriptAlias /cgi-bin/ "somepath" # ScriptAlias /cgi-bin/ "otherpath" I want to add a line only after the last occurence resulting in # ScriptAlias /cgi-bin/ "somepath" # ScriptAlias…
Luca Borrione
  • 15,077
  • 5
  • 48
  • 61
4
votes
4 answers

How to replace the last occurence of the character in .net

i want to replace the last occurence of
  • to add class inside it, it should look like below.
  • Vislink Acquires Gigawave for US$6 Million
  • Newegg Offers $25 7-inch DTV
  • The Hope…
  • Abbas
    • 4,613
    • 27
    • 88
    • 152
    3
    votes
    2 answers

    Prolog - replacing list elements

    So I have a program that has a predicate to replace the first occurrence of some element of a list with a given new element and produce a new list. I have it done like so: changeFst(OldE,[OldE|T],NewE,[NewE|T]):-!. …
    Fatalgoddess
    • 119
    • 8
    3
    votes
    4 answers

    Find last occurrence of comma in a string using regex in javascript

    I have a string which represents an address in Javascript, say, "Some address, city, postcode". I am trying to get the 'postcode' part out. I want to use the split method for this. I just want to know a regex expression that will find the last…
    Hamza Tahir
    • 248
    • 4
    • 12
    2
    votes
    1 answer

    REGEX for Grep to capture Name of last file in Zip Archive

    Well it has finally happened. My Google-fu has failed me. Please help... I have a batch file that goes through a directory and gets information from Comic archives (.cbz files) It generates a CSV file with the Titles, # of Pages, Resolution of last…
    Jack P.
    • 21
    • 1
    2
    votes
    4 answers

    last year occurrence from string

    I have strings like this: ACB 01900 X1911D 1910 1955-2011 3424 2135 1934 foobar I'm trying to get the last occurrence of a single year (from 1900 to 2050), so I need to extract only 1934 from that string. I'm trying with: grep -P -o…
    Kintaro
    • 139
    • 3
    • 12
    1
    2 3 4