Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Puzzled by use of .{1} in regex

Tags:

regex

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

like image 884
tjd Avatar asked Jul 28 '15 19:07

tjd


1 Answers

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

like image 77
Bohemian Avatar answered Sep 28 '22 21:09

Bohemian