I have the following text
/department-sale-palermo-page-3.html
And I need to get everything that is after the 3 hyphen(-)
The first thing I thought was to do
url = /departamento-venta-palermo-pagina-3.html
url[-13:]
and it returned me correctly
page-3.html
Now how can I solve the problem that instead of a digit the number is two digits example /department-sale-palermo-page-13.html since using the above would be returning me
agina-13.html
that would be wrong
If the urls follow the same pattern where after the third dash the page appears you could use this:
'-'
and you get a list[3:]
join
the list so that you have a textYou can do it with a regular expression:
reference:
pythonre
You can use
rfind( )
, which returns the index of the last match found, or-1
if there are none. In combination with string slicing in python:Detail:
text.rfind( '-' )
: returns the last occurrence of-
in the string.+ 1
: we do not want the position of last-
, but the next position .text[ inicio : final ]
: cuts the chain; returns a new string, from positioninicio
to positionfinal
.Advantage:
-
in the chain, and it is easy to adapt.Disadvantages:
-
.