regex - RegExp replace after -


i have link templates , need replace substrings inside of links. link templates:

  1. "/all_news"
  2. "/all_news/"
  3. "/all_news/page1"
  4. "/all_news/page1/"

all of these templates mean same thing - first page of news page without filtering.

so need to:

1st template - insert "/pagex"

2nd template - insert "pagex"

3rd , 4th templates - replace page number

is possible 1 regexp?

if yes, please me.

if no, have 2nd question: maybe possible replace after "/all_news" on "/pagex"?

i mean next logic:

  1. string started
  2. ok, see substring "/all_news"
  3. i replace after "/all_news" if nothing exist(if string ends "/all_news")
  4. i return "/all_news/pagex".

this'll it.

'/all_news/page1'.replace(/(.*\/all_news).*/,'$1' + '/pagex'); 

just 1 all.

java has lookbehind. negates need $1. solution looks like:

string result = "/all_news/page1"; string pattern = "(?<=\\/all_news).*"; system.out.println(result.replaceall(pattern,"/pagex")); 

cheers.


Comments

Popular posts from this blog

monitor web browser programmatically in Android? -

Shrink a YouTube video to responsive width -

wpf - PdfWriter.GetInstance throws System.NullReferenceException -