Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex - Search from the end of the string

Tags:

regex

php

How do i target something from the end of a string :
complexthing---50 other-_-50 MORE__------50

i want to end up with (50) but this "complexthing--" can end with - so in this case i cant break at " - " from the begging that might make it be confused so the easiest regex for me is to make it get whatever at the end and break at " - "

-(.*)

there is always a - before every 50 to seperate the number and the string, i want to target the number (50)

like image 861
Osa Avatar asked Oct 12 '12 21:10

Osa


People also ask

How do you match the end of a line in regex?

Line Anchors In regex, anchors are not used to match characters. Rather they match a position i.e. before, after, or between characters. To match start and end of line, we use following anchors: Caret (^) matches the position before the first character in the string. Dollar ($) matches the position right after the last character in the string. 2.

How do you use regex in Python?

Python regex re.search() method looks for occurrences of the regex pattern inside the entire target string and returns the corresponding Match Object instance where the match found.. The re.search() returns only the first match to the pattern from the target string. Use a re.search() to search pattern anywhere in the string.

What is regex in SQL?

A RegEx, or Regular Expression, is a sequence of characters that forms a search pattern. RegEx can be used to check if a string contains the specified search pattern.

How to perform search and replace operation in Python using regex?

If you want to perform search and replace operation in Python using regex, please use the re.sub () method. Both search and findall method servers the different purpose/use case when performing regex pattern matching in Python. As you know, the search method scans the entire string to look for a pattern and returns only the first match.


2 Answers

If I understand you correctly, something like this might work:

-(\d+)$

Match hyphen (-) followed by one or more digits (\d+) just before the end of the string ($). Capture the digits only ( (...) ).

like image 169
sync Avatar answered Sep 24 '22 17:09

sync


Think about what you're asking for:

  • A number which is always at the end of the string
  • A number which immediately follows a - character.
  • Something which follows a - character, which is not, itself, a - character.

These are all different ways of phrasing your question, getting less-specific as we go. Any of these would probably suit your needs, but I'll cover each case to show how it does what is does.

These can be spelled in a number of different ways. Depending on the regex flavour you're using, some of these ways may need to be escaped differently (for example, X+ vs X\+ vs XX*), and not all may be supported in all flavours.

"A number which is always at the end of the string":

([0-9][0-9]*)$
([0-9]+)$
(\d+)$

Regular expression patterns are greedy, that is: they match as much as they can. So in the first case, asking for at least one number at the end of the string ("at the end of the string" specified as $), will end up giving you the entire number at the end of the string.

"A number which immediately follows a - character":

-([0-9][0-9]*)
-([0-9]+)
-(\d+)

Here, rather than specifying the "end of the string", we specify that it should follow a - character. Because [0-9] will not match a - character itself, we don't need to do anything else to avoid capturing -.

"Something which follows a - character, which is not, itself, a - character":

-([^-][^-]*)
-([^-]+)

This one's a bit trickier: Rather than specifying we want a number, we just specify that we don't want a dash, by specifying a negative match ([^X]). Sometimes it can be tricky to match a - character in a character class, but the rule is "always specify - at the beginning", and you should be fine.

like image 32
Will Palmer Avatar answered Sep 22 '22 17:09

Will Palmer