Regex: Keep last word after text

757 Views Asked by At

Tommy Hilfiger Men Teal Blue Analogue Watch TH1791293_BBD

I need to split this and keep the last part i.e

TH1791293_BBD

The issue is that the part before the target string i.e

Tommy Hilfiger Men Teal Blue Analogue Watch

Can be of varying length.

Summary: I need to split the text using spaces and keep the last part of the array. Additional constraint: I need to do it in a single line without being able to save the object in a variable(something like this):

a= $e.split(" "); output= a[a.length()-1]

Any solution to this problem is welcome. I've just started using Parsehub for data parsing.

2

There are 2 best solutions below

5
Wiktor Stribiżew On BEST ANSWER

With Extract tool, you may use

(\S+)$

Here,

  • (\S+) - matches and captures into Group 1 one or more chars other than whitespace
  • $ - at the end of the string.

Note you should use the capturing group, see ParseHub Help Center reference:

You must use captures "()" for any text you want included in your results. Optionally, you can turn on "extract all occurrences". This will make the extract command return a list of matches.

0
Yassin Hajaj On

If you just need the last part of the string as output, the split is not even necessary, you can achieve this using the following and replace by $1 (1st matching group)

^.*?(?<=\s)([^\s]+)$

Demo

The regex actually looks actually for everything that has no space in it after the last space of the line (this is done by checking wheter the word is before to the end of the line and by using a positive-lookahead on a space character)

In java : string.replaceAll("^.*?(?<=\\s)([^\\s]+)$", "$1");