Replace text that ends with number



  • In a txt file , I want to replace string in BOLD

    just a random text/live/y8mJepPsAk/UgZtTKd6A8/79953
    just a random text/live/SvSvuq6JV5/eE3dg0bAMr/79954
    just a random text/live/eME6hoeNV9/PJa5HwhUnF/88974
    just a random text/live/bUue7Vprac/sue0ZmGzwy/78385
    just a random text/live/UznB0niAEp/MgspVk69tX/93994

    I tried this code :

    Replace : live/.*?/[0-9]
    Replace with : live/my-new-text/

    But the problem is that the first number after “/” is deleted.



  • Hello, @anass-chawki, and All

    Not very difficult, indeed, with regular expressions !

    • Open the Replace dialog ( Ctrl + H )

    SEARCH (?-s)(?<=live/).+(?=/\d+)

    REPLACE my-new-text

    • Select the Regular expression search mode

    • Tick the Wrap around option

    • Click on the Replace All button

    Notes :

    • First, the (?-s) modifier forces the regex engine to interpret any . dot as matching a single standard character only ( not an EOL character )

    • The middle part .+ matches any non-empty range of standard characters but ONLY IF the two look-around features, below, ( conditions ) are true :

      • (?<=live/) : The range .+ must be preceded with the string live/

      • (?=/\d+) : The range .+ must be followed with the string “/” and any non-empty range of digits

    • In replacement, this range, between the two / chars, is simply replaced with the my-new-text string


    An other syntax could be :

    SEARCH (?-s)live/.*/([0-9]+)

    REPLACE live/my-new-text/\1

    Notes :

    • This time the string live/ is part of the overall match, as well as the slash and the final number

    • As this number, [0-9]+, is embedded in parentheses, it is stored as group 1

    • In replacement, we rewrite the live/my-new-text/ string, followed with the group1, containing the number

    Best Regards,

    guy038



  • @guy038 Thank you for your help . Your answer is correct but I did a mistake by not puting all the line .

    http://mywebsite.net:8080/live/JHsN4SGBS7/2MOLFqluEk/102381.ts|user-agent=VLC/2.2.6 LibVLC/2.2.6

    Can you please edit your answer ?



  • @Anass-Chawki ,

    When asking for help with regular expressions, it is quite useful to include detailed specs as to what should and should not match.

    A couple of probing questions about your actual requirements:

    1. All your examples show the two “replaced words” as 10 characters each.
      • Are there always going to be exactly two “replaced words”, or could it sometimes be 1, usually 2, and sometimes 3 or more?
      • Are there always going to be exactly 10 characters in each of the “replaced words”? or can it vary from x to y? Or is it always at least 2? or …
    2. For the numbers at the end, your first example shows only 5 digits, but your updated example has 6 digits
      • Are there always going to at least 5 numbers? Or always exactly 5-6? Or …?
    3. Are there any other restrictions (this matches, but that doesn’t) or edge cases that you can think of to share?


  • Hi, @anass-chawki, and All

    Ah, OK ! The changes to do are not that important ;-))

    From your last example, I, then, supposed that the match is the shortest range of characters between :

    • The literal string live/

    • A string made up of, at least, five consecutive digits

    So, my two previous search regexes become :

    SEARCH (?-s)(?<=live/).+?(?=/\d{5,})

    and :

    SEARCH (?-s)live/.*/([0-9]\d{5,})

    Cheers,

    guy038

    P.S. :

    • I updated my two previous replacement regexes, which were misspelled !

    • The regexes look for the shortest non-empty range of characters ( .+? ), between the two limits, to avoid a wrong match against this kind of data :

    http://mywebsite.net:8080/live/JHsN4SGBS7/2MOLFqluEk/102381.ts|user-agent=VLC/2.2.6 /123456LibVLC/2.2.6.


Log in to reply