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).

4
  • To get 5th field use: /^\s*(?:\S+\s+){4}(\S+)/ Commented Sep 13, 2019 at 17:57
  • That's what I meant, thank you so much! Commented Sep 13, 2019 at 18:01
  • See for example stackoverflow.com/questions/23555593/… and stackoverflow.com/questions/37751545/… Commented Sep 13, 2019 at 18:06
  • 1
    @anubhava Could you please make an answer? I want this out of the list of unanswered questions. Commented Sep 13, 2019 at 20:39

2 Answers 2

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+)/
Sign up to request clarification or add additional context in comments.

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.