-1

This is my string

hello my friend, i want to say hello to all of you

I am using following regex to match hello to all of you But for some reason, it is matching entire string.

hello(.*?)you

I think because I am using wild card match. I want start to be hello and end to be you and get anything between that, but not the entire string.

My regex completely ignoring inner hello word and giving me broad match. I want it to be narrow.

  • 2
    Hi and welcome to Stack Overflow! To improve your experience, read [how to ask](https://stackoverflow.com/help/how-to-ask) an [on-topic](https://stackoverflow.com/help/on-topic) question. You should also take a look at the [question checklist](https://meta.stackoverflow.com/questions/260648/stack-overflow-question-checklist) and [how to create a Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve). To lean more about how Stack Overflow works, [take the tour](https://stackoverflow.com/tour). – jsadev.net Oct 28 '19 at 13:57
  • 1
    "I want start to be hello and end to be you and get anything between that" That is exactly what you got :P – LogicalKip Oct 28 '19 at 14:00
  • If your criterion is "not from the start", you can use `(?<!^)` before your regex. It means "not preceded by the start of the string" – LogicalKip Oct 28 '19 at 14:02
  • Criteria is not about start or end... i just want to narrow the match. I do not want it to consider first hello word as there is already 2nd hello word after that. Actual string is around 500-600 in words. – jokerbudy11 Oct 28 '19 at 14:23
  • Then what is your criterion ? Why is the second hello ok but not the first ? What if there are plenty of other hello between them, which should be chosen and why ? – LogicalKip Oct 28 '19 at 14:40
  • pick which is the closest before you word and ignore all others. I hope it will make clear what i am looking for. – jokerbudy11 Oct 28 '19 at 15:15
  • You could use negative lookaheads so that the inner text only matches anything that doesn't have "hello" in it, giving you this regex: `/hello(?:.(?!hello))*?you/` It works on your sample string, and I think it should work on all such strings. – IceMetalPunk Oct 28 '19 at 15:47
  • @IceMetalPunk Thank you only your solution worked for me – jokerbudy11 Oct 31 '19 at 02:50

3 Answers3

1

Your regex is working correctly. Your string starts with hello and finishes with you. One way to prevent this is to restrict the character you want to match, for example your comma. this way it will only match the last part of the string.

(hello(\w| )+you)

Here i'm only matching words, so the comma does not count.

You can try more case here

Nicolas
  • 8,077
  • 4
  • 21
  • 51
  • thanks, but this is just a demo string. Actual string is around 500-600 in words, its having a lot of special characters and punctuation, so this logic will fail. I just want to go for narrow match rather than broad match. Since in given string there is already 2nd hello word, i do not want it to consider 1st one... – jokerbudy11 Oct 28 '19 at 14:20
0

Here you go: .*(hello.+you).* (js format with case insensitive: var regex=/.*(hello.+you).*/i)

Detailed here: Regex101

In the link I use g+m flag so it could match more, without it should only match the first line that have the occurrence, if you want to match multilines use var regex=/.*(hello.+you).*/igm, but idk what would happen to the group data selector when you do that tho, not an expert, just googling and try-and-error modifying the regex to find the answer for you lol, hope it helps.
p/s: Replace the center .+ with .* if you want allow next-to matching, ie: helloyou.

0

If you're looking to get the last 'hello', until the last 'you', you can use

.*(hello.*you)

and your sentence will be in group 1 because of the greedy .*

LogicalKip
  • 514
  • 4
  • 13
  • This matches the whole test string and you have to fetch the group, I don't know what regex could match only the "hello.*you" part. If anyone can help on this theoretical problem I'm interested. – LogicalKip Oct 28 '19 at 14:59
  • LogicalKip the regex I posted in a comment to the original question above will do it, using negative lookaheads: `/hello(?:.(?!hello))*you/` – IceMetalPunk Oct 28 '19 at 15:48