2

i have a problem and i don't undestand how to do this ( after 6hours or googling)

i'have a string named "filename" containt this text :"Aachen-Merzbrück EDKA\r\r\nVerkehr" i want to use regex to only get this part "Aachen-Merzbrück EDKA" but i cant....

here my code :

NSString *expression = @"\\w+\\s[A-Z]{4}";

NSError *error = NULL;

NSRegularExpression *regex = [NSRegularExpression         regularExpressionWithPattern:expression options:NSRegularExpressionCaseInsensitive error:&error];

NSString *noAirportString = [regex stringByReplacingMatchesInString:filename options:0 range:NSMakeRange(0, [filename length]) withTemplate:@""];

EDIT :

this one work good : \S+\s+[A-Z]{4}

but now, how to get only this "Aachen-Merzbrück" EDKA from "Aachen-Merzbrück EDKA\r\r\nVerkehr"

my regex with NSRegularExpression return me the same string ....

Jeffrey
  • 452
  • 1
  • 9
  • 22
  • I don't believe the word character class `\w` will match on a hyphen. – vcsjones Oct 08 '13 at 14:34
  • no difference with : NSString *expression = @"\w+\s[A-Z]{4}"; – Jeffrey Oct 08 '13 at 14:41
  • Try `@"\\.*?\\s[A-Z]{4}"` – vcsjones Oct 08 '13 at 14:48
  • try end of line (you have `\n` character after your match): "^.?+$" – Marek R Oct 08 '13 at 14:50
  • maybe it works but : ` NSString *noAirportString = [regex stringByReplacingMatchesInString:filename options:0 range:NSMakeRange(0, [filename length]) withTemplate:@""];` return me the same string, maybe it's false ? – Jeffrey Oct 08 '13 at 14:56
  • it complicate because i have 3 possibility of txt : AALEN-HEIDENHEIM/ELCHINGEN EDPA or Aachen-Merzbrück EDKA or Mönchengladbach EDLN – Jeffrey Oct 08 '13 at 15:01
  • this one work good : \S+\s+[A-Z]{4} but now, how to get only this "Aachen-Merzbrück" EDKA from "Aachen-Merzbrück EDKA\r\r\nVerkehr" my regex with NSRegularExpression return me the same string .... – Jeffrey Oct 08 '13 at 15:21

2 Answers2

0

A couple of issues in your question:

  1. No need to match city name characters - there are always weird ones around (hyphens, apostrophes, etc.) You can just match the first "line" in your text with a test for the ICAO code as an extra security.
  2. Using stringByReplacingMatchesInString: you actually remove the airport name (and ICAO code) that you want keep.
  3. stringByReplacingMatchesInString: is a hacky (because it deletes things, so you need to make your regexes "negative") shortcut that sometimes works (I use it myself) but which risks confusing things - and future readers.

Having said that, a few changes will fix it:

NSString *filename = @"Aachen-Merzbrück EDKA\r\r\nVerkehr";

// Match anything from the beginning of the line up to a space and 4 upper case letters.
NSString *expression = @"^.+\\s[A-Z]{4}$";

NSError *error = NULL;

//Make sure ^ and $ match line endings, 
//and make it case sensitive (the default) to explicitly 
//match the 4 upper case characters of the ICAO code
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:expression options:NSRegularExpressionAnchorsMatchLines error:&error];

NSArray *matches = [regex matchesInString:filename
                                  options:0
                                    range:NSMakeRange(0, [filename length])];

// Check that there _is_ a match before you continue
if (matches.count == 0) {
    // Error
}

NSRange airportNameRange = [[matches objectAtIndex: 0] range];
NSString *airportString = [filename substringWithRange: airportNameRange];
Monolo
  • 18,205
  • 17
  • 69
  • 103
0

Thanks it's good working, but i use this one, it's work better in my case :

NSString *expression = @"\\S+\\s+[A-Z]{4}";
Jeffrey
  • 452
  • 1
  • 9
  • 22
  • Sure, there are many ways to build regexes to solve your problem. Notice, though, that your regex pattern will not produce the desired result for airports with spaces in their name, e.g. "Schwäbisch Hall EDTY", not to mention "John F. Kennedy KJFK" or other airports named after famous persons - they often have spaces in their names. – Monolo Oct 09 '13 at 09:21