2

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 Answers2

2

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");

Yassin Hajaj
  • 21,337
  • 9
  • 51
  • 89
2

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.

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
  • 2
    @YassinHajaj Yeah, almost all of the shorthand character classes have there "negated" counterparts. `\D` != `\d`, `\W` != `\w`. – Wiktor Stribiżew May 14 '18 at 09:50
  • Thank you for this! What about splitting it by - (negative sign)? I tried '(//-+)$', '(/-+)$', '([-]+)$' – Ishaan Shakunt May 17 '18 at 04:31
  • @IshaanShakunt No idea what you mean. `Tommy Hilfiger Men Teal Blue Analogue Watch TH1791293_BBD` does not contain a single `-`. If you have a string that you want to split with `-`, do it. – Wiktor Stribiżew May 17 '18 at 06:37
  • Examples: "Fastrack Casual Analog White Dial Men's Watch - 3120SL01", "Titan Karishma Analog Multi-Colour Dial Men's Watch -NK9151YM01". I only want the serial number at the end. Since the number of '-' is varying, I'll need the string at the end only. – Ishaan Shakunt May 17 '18 at 10:03
  • @IshaanShakunt You may exclude the hyphen as the first char matched . Use [`r'[^-\s]\S*$'`](https://regex101.com/r/77I2Hm/1). – Wiktor Stribiżew May 17 '18 at 10:07