1

I want to select the nth non-whitespace regex match. I have a txt file (string) with some data. Each data is separated by a space.

My regular expression find every match: /\S+/g

String:

12345 - - - 23.6 32 1003.0 0.00 3.20 28.60 0.00 0.00 25.8 21 - 2 - - 0 0.00 23.0 - - - 23.1 22.2 - - - 16 54 14 Ĺagiewniki_M.-16:54 0 - 12 09 - - - - - - - - 23.2 23.7 8.6 2 - -1 - - - - - - - - - - - - - - - - - - - - - 5.9 7080 12/09/2019 24.1 8.7 - - - - - - - - - - - - - 22.8 23.2 22.9 21.6 20.3 18.5 16.3 12.7 9.9 8.8 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 23.7 8.6 23.6 - 0 _ _ - _ 0 - - - - - - - 0 25.8 23.2 - 1005.0 1001.0 - --:-- --:-- - - 13.6 5.3 -.- 2019 _ 0 0 0 - - - - - - - - - - - - - - 50.683056 -18.51 - 95.0 31.0 - --:-- !!C10.37Of!!

I used https://regex101.com/ and I see the Math 1, 2 e.t.c on the site, but I can't find only one that I want to find (for example 5th match).

TylerH
  • 20,799
  • 66
  • 75
  • 101

2 Answers2

3

You may use this regex to get 5th non-whitespace filed:

/^\s*(?:\S+\s+){4}(\S+)/

RegEx Demo

RegEx Details:

  • ^\s*: Match 0 or more whitespace at start
  • (?:: Start non-capture group
    • \S+: Match 1+ non-whitespace characters
    • \s+: Match 1+ whitespace characters
  • ){4}: End non-capture group. Match 4 instances of this non-capture group.
  • (\S+): Match and capture 1+ non-whitespace characters in group #1

In general to get any Nth value use this regex by replacing N accordingly:

 /^\s*(?:\S+\s+){N}(\S+)/
anubhava
  • 761,203
  • 64
  • 569
  • 643
0

A repeated capturing group will only capture the last iteration.

It can be even simpler if you use a repeated capturing group. Only the last captured group is caught. The match will be in the first capture group returned by the regex resolution. Only catch is that this particular regex returns the space too, so if you don't need that, you'll have to trim it off.

/(\S+\s){5}/ Group 1. 23.6

Demo

Joseph Marikle
  • 76,418
  • 17
  • 112
  • 129