0

Does anyone know how to say I can get a regex (C#) search of the first 3 letters of a full name?

Without the use of (.*)

I used (.**)but it scrolls the text far beyond the requested name, or if it finds the first condition and after 100 words find the second condition he return a text that is not the look, so I have to limit in number of words. Example: \s*(?:\s+\S+){0,2}\s*

I would like to ignore names with less than 3 characters if they exist in name.

Search any name that contains the first 3 characters that start with:

'Mar Jac Rey' (regex that performs search)

Should match:

  • Marck Jacobs L. S. Reynolds
  • Marcus Jacobine Reys
  • Maroon Jacqueline by Reyils

Can anyone help me?

p.s.w.g
  • 136,020
  • 27
  • 262
  • 299
Marco Araujo
  • 155
  • 11
  • Your input/output and regex are confusing, please edit and rephrase the question. – hwnd Feb 10 '15 at 21:37
  • I am using C# Language – Marco Araujo Feb 10 '15 at 21:44
  • changed the text for better comprehension – Marco Araujo Feb 10 '15 at 21:49
  • As per [this discussion on Meta](http://meta.stackoverflow.com/questions/285733/should-give-me-a-regex-that-does-x-questions-be-closed?cb=1) regarding questions asking for "a regex to do X", this question is flagged as a duplicate of [the canonical regex reference question](http://stackoverflow.com/questions/22937618/reference-what-does-this-regex-mean). – anaximander Feb 11 '15 at 14:11

3 Answers3

0

I think what you want is this regular expression to check if it is true and is case insensitive

@"^[Mar|Jac|Rey]{3}"

Less specific:

@"^[\w]{3}"
Drifter
  • 281
  • 1
  • 9
0

If you want to capture the first three letters of every words of at least three characters words you could use something like :

((?<name>[\w]{3})\w+)+

And enable ExplicitCapture when initializing your Regex.

It will return you a serie of Match named "name", each one of them is a result.

Code sample :

Regex regex = new Regex(@"((?<name>[\w]{3})\w+)+", RegexOptions.ExplicitCapture | RegexOptions.IgnoreCase);
var match = regex.Matches("Marck Jacobs L. S. Reynolds");

If you want capture also 3 characters words, you can replace the last "\w" by a space. In this case think to handle the last word of the phrase.

AFract
  • 6,601
  • 6
  • 39
  • 61
0

The zero or more quantifier (*) is 'greedy' by default—that is, it will consume as many characters as possible in order to finding the remainder of the pattern. This is why Mar.*Jac will match the first Mar in the input and the last Jac and everything in between.

One potential solution is just to make your pattern 'non-greedy' (*?). This will make it consume as few characters as possible in order to match the remainder of the pattern.

Mar.*?Jac.*?Rey

However, this is not a great solution because it would still match the various name parts regardless of what other text appears in between—e.g. Marcus Jacobine Should Not Match Reys would be a valid match.

To allow only whitespace or at most 2 consecutive non-whitespace characters to appear between each name part, you'd have to get more fancy:

\bMar\w*(\s+\S{0,2})*\s+Jac\w*(\s+\S{0,2})*\s+Rey\w* 

The pattern (\s+\S{0,2})*\s+ will match any number of non-whitespace characters containing at most two characters, each surrounded by whitespace. The \w* after each name part ensures that the entire name is included in that part of the match (you might want to use \S* instead here, but that's not entirely clear from your question). And I threw in a word boundary (\b) at the beginning to ensure that the match does not start in the middle of a 'word' (e.g. OMar would not match).

p.s.w.g
  • 136,020
  • 27
  • 262
  • 299