9

In a couple recent questions here on Stack Overflow, I've seen the regex sequence .{1}. In the regex engines with which I'm familiar, a repeat count of 1 is strictly redundant.

Is there a regex engine that I am unaware of for which this is not true?

Could this explicit count of 1 be an attempt to comment/highlight that the preceding . is a metacharacter?

I'm just trying to understand the motivations for this practice.


Links: Regex with $ anchor and look ahead is the most recent... looking for the other...

Community
  • 1
  • 1
tjd
  • 4,064
  • 1
  • 24
  • 34
  • 4
    It can very well be single dot. I'm not aware of any regex engine where `.{1}` is different from `.` – anubhava Jul 28 '15 at 19:16
  • @Jongware That is a plausible explanation... – tjd Jul 28 '15 at 19:20
  • 7
    @Jongware: I imagine you meant "You can put `{1}` _after_ anything", rather than _before_. Do you want to delete and re-create your comment so it can be upvoted? – Jonathan Leffler Jul 28 '15 at 19:25
  • Can you link those 'recent questions', so we can look at the occurrences in-context? – das-g Jul 28 '15 at 19:33
  • 3
    It would be better to link to questions where this is used in the **answer**, not the question. People who write questions often don't know what they're doing, so you shouldn't take their code as examples of good coding. – Barmar Jul 28 '15 at 19:53
  • I'm not sure I've seen it in an answer, but the fact that I'd seen it repeated in questions made me think there's a sub-culture with which I am unfamiliar. – tjd Jul 28 '15 at 19:57
  • 2
    You can put `{1}` after anything, metacharacter or not. "In a couple recent questions here" suggests Magical thinking and/or Cargo cult programming. A bit like adding backslashes before just about anything. (Magical Thinking = "it worked elsewhere so it must also work here"; Cargo Cult Programming = "I copied this fragment from a working example".) – Jongware Jul 28 '15 at 23:06
  • Related: [Does the quantifier {0} make sense in some scenarios?](http://stackoverflow.com/q/7511600/7586) – Kobi Aug 04 '15 at 05:59

1 Answers1

5

Putting {1} after any repeatable term has no effect whatsoever.

I could understand if {1} appeared in a regex that was generated using a variable for the count of a term, eg:

String regex = "foo.{" + n + "}bar";

to match "foo" and "bar" separated by n characters. When n happened to be 1, you would get "foo.{1}bar".

Bohemian
  • 412,405
  • 93
  • 575
  • 722
  • I can tell you that I have done exactly this sort of thing in the past, where I had to write code to take some sort of proprietary format string and convert it into regex. I would end up with a counter for the number of consecutive occurrences, and that would get converted to `"{" + count + "}"`. When `count == 1`, it was easier to just leave it in, since the regex engines parsed it correctly and it required no modification to my code. – Douglas Barbin Jul 28 '15 at 20:02
  • Fair answer (+1), but I'm not sure it matches the context in which I'd seen it. – tjd Jul 28 '15 at 20:03
  • 3
    That's really the only context where it makes any sense that you would have something like that. Otherwise, it's just cruft. – Douglas Barbin Jul 28 '15 at 20:06
  • The same argument can be said for `{0}`. It and the token before is redundant, but the construct allows for generated regex to work without having to deal with conditions and stuffs. – nhahtdh Jul 29 '15 at 03:33