5

In the app I use, I cannot select a match Group 1.

The result that I can use is the full match from a regex.

but I need the 5th word "jumps" as a match result and not the complete match "The quick brown fox jumps"

^(?:[^ ]*\ ){4}([^ ]*)

The quick brown fox jumps over the lazy dog

Here is a link https://regex101.com/r/nB9yD9/6

Dave2e
  • 22,192
  • 18
  • 42
  • 50
gsxr1300
  • 351
  • 2
  • 4
  • 12
  • 2
    What language/tool are you using? Does it support variable length lookbehind? – Toto Jan 24 '19 at 16:24
  • the app is ecoDMS 18.09 the programming language as i know is qt 5.7, i dont know if it supports lookbehind, i can try it, can you show me a sample regex? – gsxr1300 Jan 24 '19 at 16:32
  • Have a try with `^(?<=(?:\S+ ){4})\S+` or `^(?:\S+ ){4}\S+` – Toto Jan 24 '19 at 17:10
  • i try this "(?<=(\b\s))(\w*)" and it gives me the second word "quick" but how can i get the third word "brown" and the so on "fox" ... – gsxr1300 Jan 24 '19 at 18:19
  • Which word do you need? In the question you've said the 5th but in comment you say the 3rd, could you clarify by [editing the question](https://stackoverflow.com/posts/54351071/edit)? – Toto Jan 25 '19 at 14:23
  • i need all words from a string with six words, for every word i need a single pattern, the only restriction is, that the app takes only the full match from regex, no group1 group2 etc – gsxr1300 Jan 25 '19 at 16:50
  • If I well understand, you want a regex for the first word, another one for the second and so on? 1srt: `\w+`, 2nd: `^(?<=\w+ )\w+`, 3rd: `^(?<=(?:\w+ ){2})\w+`, 4th: `^(?<=(?:\w+ ){3})\w+` and so on... Is that what you want? – Toto Jan 26 '19 at 09:37
  • Yes, that is what i need, but if i use something like this "^(?<=(?:\w+ ){2})\w+" i got "? The preceding token is not quantifiable" – gsxr1300 Jan 28 '19 at 09:54
  • I guess your app doesn't support variable length lookbehind. Have a try with: `^\w+ \K\w+` and `^(?:\w+ ){2}\K\w+` and so on... – Toto Jan 28 '19 at 10:00
  • \K like this "^(?:\w+ ){2}\K\w+" does not work, without it like this "^(?:\w+ ){2}\K\w+" i got a full match with three words. ( https://regex101.com/r/pR22LK/1 ) – gsxr1300 Jan 28 '19 at 13:28
  • 1
    It works https://regex101.com/r/pR22LK/2 with PCRE. Your app doesn't seem to support it, but I don't know how it works. I think you have to extract all the words in an array then select the ones you want. – Toto Jan 28 '19 at 14:02
  • Hello Toto, your solution works in the the App too, like PCRE, thanks !!! – gsxr1300 Jan 29 '19 at 13:28

4 Answers4

6

Since you need the entire match to be only the n-th word, you can try to use 'positive lookbehind', which allows you to only match something, if it is preceded by something else.

To match only the fifth word, you want to match the first word that has four words before it.

To match four words (i.e. word characters followed by a space character):

(\w+\s){4}

To match a single word, but only if it was preceded by four other words:

(?<=(\w+\s){4})(\w+)

Test the result here https://regex101.com/r/QIPEkm/1

Grismar
  • 27,561
  • 4
  • 31
  • 54
  • ? The preceding token is not quantifiable ,Your pattern contains one or more errors, please see the explanation section above. – gsxr1300 Jan 25 '19 at 07:00
  • 1
    The regex functions just fine for me on regex101, in RegexBuddy and in a simple Python script. I'm assuming you changed something that broke it? – Grismar Jan 29 '19 at 04:01
2

To find the 3rd word of sentence, use:

^(?:\w+ ){2}\K\w+

Explanation:

^               # beginning of line
    (?:         # start non capture group
        \w+     # 1 or more word character
                # a space
    ){2}        # group must appear twice (change {2} in {3} to get the 4th word and so on)
    \K          # forget all we have seen until this position
    \w+         # 1 or more word character

Demo

Toto
  • 89,455
  • 62
  • 89
  • 125
1

It works https://regex101.com/r/pR22LK/2 with PCRE. Your app doesn't seem to support it, but I don't know how it works. I think you have to extract all the words in an array then select the ones you want. – Toto 23 hours ago

Hello Toto, your solution works in the the App too, like PCRE, thanks !!! – gsxr1300 just now edit

gsxr1300
  • 351
  • 2
  • 4
  • 12
0

To match "the first" four words (i.e. word characters followed by a space character):

^(\w+\s){4}

To match a single word, but only if it was preceded by "the first" four other words:

(?<=^(\w+\s){4})(\w+)

Note the ^ difference

If you want to know what this "?<=" mean, check this: https://stackoverflow.com/a/2973495/11280142

  • Can you [edit] your answer and explain the difference? What does `?<=` do in a regular expression and why is that needed here? – Robert Mar 23 '21 at 21:59