2

i am a regular expression newbie. I have a working code using nsregularexpression. i am modifying it a little.

__nameRegularExpression = [[NSRegularExpression alloc] initWithPattern:@"^\\w+" options:NSRegularExpressionCaseInsensitive error:nil];

@"^\w+" what does it refer to ? does it convert first word to capital ?

I have a \r\n in first line of the text. i need to get NSRange till that and i dont want to change it to caps.

please suggest solutions.

Alan Moore
  • 73,866
  • 12
  • 100
  • 156
sununest
  • 65
  • 1
  • 7

1 Answers1

1

\w means match a word character. (the double '\' is just escaping a single '\'.

\w+ means match one or more word characters. Assuming greedy matching it will match as many word characters as possible (longest match).

Specifically \w means unicode

[\p{Ll}\p{Lu}\p{Lt}\p{Lo}\p{Nd}]

which means (in order)

Letter lowercase, Letter uppercase, Letter titlecase, Letter other, Number decimal digit.

zaph
  • 111,848
  • 21
  • 189
  • 228
  • does that mean this regular expression is not converting the word to capital letter ? what does ^\ in the beginning stand for ? – sununest Sep 06 '11 at 12:24
  • It just matches, it does not convert. To convert case see the NSString methods: lowercaseString, uppercaseString and capitalizedString. – zaph Sep 06 '11 at 12:31
  • thanks, but this regex gives me first word. i have string like first line text1 first line text2 \r\n secondline text 1 .... I want to find only till \r\n please help – sununest Sep 06 '11 at 12:37
  • or else if i can get till first ? character found i will be fine – sununest Sep 06 '11 at 12:51
  • i found @"[\?]" does affect my changes to the only character ? i am close ! i need it find everything before it found ? first ! how to do that ? – sununest Sep 06 '11 at 12:58
  • use: @"[^\\r]+" anything not \r up to \r. – zaph Sep 06 '11 at 12:59