0

I'm new to Regex. I try to build a NSScanner using Regex in my iOS 7 and Parse String and Data . I try to understand how does this regex and pattern matching work.
Note: I only try to deal with one of cases: email ( name )

My friend suggests me to try this code. My question is that how does the following regex accomplish that?

  NSString *someRegexp = @".*[\\s]*\\({1}(.*)"

Here are some Testing examples:

// chang0002@student.tc.umn.edu (Jeff Chang)
// mbuntan@staff.tc.umn.edu ()
JeffreyC
  • 625
  • 1
  • 8
  • 19
  • 1
    It captures anything followed by zero or more spaces followed by a single "(", then captures the rest of the line into capture group 1. In your example cases it would capture "Jeff Chang)" and ")" into the group. However, the RegEx could be simplified to: `.*\\s*\\((.*)`, but it also doesn't really match an email followed by a name in parenthesis. – tenub Mar 10 '14 at 18:52
  • This is a very poor regex for matching names/email addresses... – TypeIA Mar 10 '14 at 18:53
  • Beware of using a regex with email addresses because they can be very very complicated: http://stackoverflow.com/questions/201323/using-a-regular-expression-to-validate-an-email-address – WOUNDEDStevenJones Mar 10 '14 at 18:56
  • I would change the RegEx to: `(\\S+@.*?\\..*?)\\s*?\\((.*?)\\)`. Then you could do whatever you wanted with the captured emails and names, including simply reformatting the original string in place. – tenub Mar 10 '14 at 19:03

2 Answers2

4

Regex isn't for the faint-hearted, but once you get stuck in you'll find they really aren't that complicated. It's just that to the absolute beginner, you can't even get a faint idea idea of what's going on, compared with, say, looking at one language's way of doing something you already know how to do in another language. Anyway, this is one of the best tutorials I know of:

http://www.regular-expressions.info/tutorial.html

Hektor
  • 1,845
  • 15
  • 19
0

This is a very poor regex for matching names/email addresses. I assume that you will have to include more cases. This regular expression finds a pattern matching. It checks for zero or more occurrences of any character, followed by zero or more occurrence of a space, followed by one open parenthesis "(" finally zero or more occurrences of a string.

KJC2009
  • 59
  • 1
  • 17