1

I have a regex expression:

^([ \t\r]*\S+){2}$

It matches name and group matches e, in name.

I expected to not to match name, but name name; such as I wanted \S+ work till it matches. I can make it work just a bit changing it:

^([ \t\r]*\S+)([ \t\r]+\S+)$

but I hoped it would be possible to make it shorter.

icedwater
  • 4,701
  • 3
  • 35
  • 50
user2688153
  • 47
  • 1
  • 8

3 Answers3

1

+, *, and ? are not possessive. They will match as much as they can if that produces a valid match, but if not, the regex engine will backtrack and try matching less. I don't think Python supports possessive quantifiers; you'll have to recode your regex if you don't want to match name.

user2357112
  • 260,549
  • 28
  • 431
  • 505
  • 1
    Thank you. Such as my regex syntax is Oniguruma, it has possessive operators. My final code is `^([ \\t\\r]*\\S++){2}$`. – user2688153 Aug 22 '13 at 01:29
0

You need to pass the global modifier. I'm not sure which programming language you are using, but the syntax often resembles the following:

/$myregex/g

For example, given the following text:

Hello Adam, how are you? Hello Sarah, how are you?

The regular expression /Hello\s(.*),/g will match both Adam and Sarah.

ktm5124
  • 11,861
  • 21
  • 74
  • 119
0

With your original expression, ^([ \t\r]*\S+){2}$, na and me are matched separately as different groups since you do not force (due to the * in [ \t\r]*) a space character after the first group.

You can use a lookahead assertion:

^([ \t\r]*\S+(?!\S)){2}$

Or you can use alternation:

((^|\s+)\S+){2}$
perreal
  • 94,503
  • 21
  • 155
  • 181
  • thanks for the response, second example works the same way as my second example, but I hoped there's an easier way of doing it. First example works only if both words are equal, sorry for confusion. – user2688153 Aug 22 '13 at 01:16