0

I am trying to use Regex in notepad++ to select everything after v+(number|character)* but in the selection it should excluded the v+(num|char)*.

e.g. master\_\move_consolidate_archives_html_to_move_base_v2kjkj_(2021_01_19_11h43m59s-fi_m_dt xx-) - Copy (2).bat"

I am expecting

_(2021_01_19_11h43m59s-fi_m_dt xx-) - Copy (2).bat"

so far I can use this line (?i)(v\d[0-9a-z]*) to select v2kjkj

but I can't get this to work with lookbehind (?<=xxxx). I am also trying to use if-then-else condition but no luck for me. I am still don't understand enough to using it.

issue. because the "v" have different pattern in it. I can't hard code to certain string

v2
v23
v2kjkj
v2343434

Test string:

mmaster\_\move_consolidate_archives_html_to_move_base_v2_16_.bat"
master\_\move_consolidate_archiv es_html_to_move_base_v23_17_.bat"
master\_\move_consolidate_archives_html_to_move_base_v2_17_(2021_01_19_12h37m19s-fi_m_dt xx-).bat"
master\_\move_consolidate_archives_html_to_move_base_v2_(2021_01_19_11h43m59s-fi_m_dt xx-) - CopyCopy.bat"
master\_\move_consolidate_archives_html_to_move_base_v2kjkj_(2021_01_19_11h43m59s-fi_m_dt xx-) - Copy (2).bat"
master\_\move_consolidate_archives_html_to_move_base_v2343434_(2021_01_19_11h43m59s-fi_m_dt xx-) - Copy (3).bat"

I have been reading and searching for a day but I can't apply anything I have seen so for. the closest one I see was

  • Regexp match everything after a word
  • Getting the text that follows after the regex match

I am welcome any comments.

logi-kal
  • 7,107
  • 6
  • 31
  • 43
  • _Question 1_: is there a maximum number of `(number|character)`?. _Question 2_: if you are replacing, can't you just wrap the `v+(number|character)*` part in a capturing group and replace it as-is? – logi-kal Jan 19 '21 at 16:22
  • Ans1. maximum (number|char) not encounter so far. what I have seen just v+random char and/or number for certain string as example above Ans2. I am not sure if I understand your suggestions correctly or not, basically I want to replace/delete text after v+random+ (not the {1} itself) thank you – James Bell Jan 19 '21 at 16:48
  • What is the expected result for the test cases? – Toto Jan 19 '21 at 16:50
  • I mean you could replace `(?i)(v\d[0-9a-z]*).*` with `\1` (see demo [here](https://regex101.com/r/NwqFp8/1)). – logi-kal Jan 19 '21 at 16:50
  • your demo is amazing, thank you for showing and clarify. I have to put this expression in script eg.filename;s/exp//; from what I understand s/_expr_/_replace_/ it select the string and replace. I don't know if the replace part can put \1 or not. I will try now and let you know. – James Bell Jan 19 '21 at 17:15
  • reply:toto select everything after not include v+random eg.master\_\move_consolidate_archives_html_to_move_base_v2kjkj_(2021_01_19_11h43m59s-fi_m_dt xx-) - Copy (2).bat. I want this part **_(2021_01_19_11h43m59s-fi_m_dt xx-) - Copy (2).bat** – James Bell Jan 19 '21 at 17:18
  • @JamesBell Please notice that your script language may use a more powerful regex engine than Notepad++. See also https://en.wikipedia.org/wiki/Comparison_of_regular-expression_engines – logi-kal Jan 19 '21 at 18:03
  • your suggestions work well and thank you for more info – James Bell Jan 19 '21 at 18:10

1 Answers1

1
  • Ctrl+H
  • Find what: v\d[0-9a-z]*\K.*$
  • Replace with: LEAVE EMPTY
  • UNCHECK Match case
  • CHECK Wrap around
  • CHECK Regular expression
  • UNCHECK . matches newline
  • Replace all

Explanation:

v               # a "v"
\d              # a digit
[0-9a-z]*       # 0 or more alphanum
\K              # forget all we have seen until this position
.*              # 0 or more any character but newline
$               # end of line

Screenshot (before):

enter image description here

Screenshot (after):

enter image description here

Toto
  • 89,455
  • 62
  • 89
  • 125
  • Nice. I always forget about `\K` :-) – logi-kal Jan 19 '21 at 17:02
  • @horcrux: Thanks. `\K` is very usefull when variable-length lookbehind is not supported. – Toto Jan 19 '21 at 17:28
  • very nice presentation, clear, clean, precise, and works. thank you for share your expertise – James Bell Jan 19 '21 at 17:37
  • @JamesBell: You're welcome, glad it helps. Feel free to mark the answer as accepted, [How to accept an answer](https://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work) – Toto Jan 19 '21 at 17:46
  • two more Questions Q1. what exactly ". matches newline" do. Q2. why **Replace all work** for this code but not **replace** – James Bell Jan 20 '21 at 03:57
  • @JamesBell: 1) `. matches newline` means exactly what it says, it's equivalent to `/s` flag or `(?s)`. 2) I don't know, probably a bug! – Toto Jan 20 '21 at 09:16