Escape regular expression reserved characters?

Gabriel Goncalves

Anyone know how to escape reserved regex's characters from a NSString?

I think these are the special characters that I must escape

. ^ $ * + - ? ( ) [ ] { } \ |

Currently I'm using this to escape all characters:

    regex = [regex stringByReplacingOccurrencesOfString: @"\\" withString:@"\\\\"];
    regex = [regex stringByReplacingOccurrencesOfString: @"{" withString:@"\\{"];
    regex = [regex stringByReplacingOccurrencesOfString: @"}" withString:@"\\}"];
    regex = [regex stringByReplacingOccurrencesOfString: @"?" withString:@"\\?"];
    regex = [regex stringByReplacingOccurrencesOfString: @"+" withString:@"\\+"];
    regex = [regex stringByReplacingOccurrencesOfString: @"[" withString:@"\\["];
    regex = [regex stringByReplacingOccurrencesOfString: @"(" withString:@"\\("];
    regex = [regex stringByReplacingOccurrencesOfString: @")" withString:@"\\)"];
    regex = [regex stringByReplacingOccurrencesOfString: @"^" withString:@"\\^"];
    regex = [regex stringByReplacingOccurrencesOfString: @"$" withString:@"\\$"];
    regex = [regex stringByReplacingOccurrencesOfString: @"|" withString:@"\\|"];
    regex = [regex stringByReplacingOccurrencesOfString: @"/" withString:@"\\/"];

but that's too expensive.

Thanks.

Gabriel Goncalves

So, for those who want a simple way to do this you can check NSRegularExpression class here

There you'll find some methods to do this, one of them is:

+ escapedTemplateForString:

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Regular expression to parse escape characters

From Dev

How to escape characters in a regular expression

From Dev

Regular expression to parse escape characters

From Dev

Parsing for escape characters with a regular expression

From Dev

Regular expression to escape regular expressions

From Dev

Accented characters and regular expression

From Dev

Regular Expression for special characters

From Dev

regular expression to find until characters?

From Dev

Javascript regular expression to capture characters

From Dev

Special Characters in JFlex Regular Expression

From Dev

Writing a regular expression with special characters

From Dev

Regular expression get last characters

From Dev

Python Regular Expression with special characters

From Dev

regular expression match digit and characters

From Dev

Matching special characters with Regular Expression

From Dev

Actionscript regular expression for repeating characters

From Dev

PHP Regular Expression special characters

From Dev

Regular expression get last characters

From Dev

Regular expression that checks if characters are escaped

From Dev

Special Characters in JFlex Regular Expression

From Dev

regular expression match digit and characters

From Dev

Regular expression for special characters and numericals

From Dev

Regular expression that ignores certain characters

From Dev

replacing characters with javascript regular expression

From Dev

Is there a Regular Expression to find characters in sequence?

From Dev

Regular expression with drools (special characters /* )

From Dev

Regular expression for anything except ']]' characters

From Dev

Regular Expression exact count of characters

From Dev

Trying to escape period in JavaScript regular expression but failing

Related Related

  1. 1

    Regular expression to parse escape characters

  2. 2

    How to escape characters in a regular expression

  3. 3

    Regular expression to parse escape characters

  4. 4

    Parsing for escape characters with a regular expression

  5. 5

    Regular expression to escape regular expressions

  6. 6

    Accented characters and regular expression

  7. 7

    Regular Expression for special characters

  8. 8

    regular expression to find until characters?

  9. 9

    Javascript regular expression to capture characters

  10. 10

    Special Characters in JFlex Regular Expression

  11. 11

    Writing a regular expression with special characters

  12. 12

    Regular expression get last characters

  13. 13

    Python Regular Expression with special characters

  14. 14

    regular expression match digit and characters

  15. 15

    Matching special characters with Regular Expression

  16. 16

    Actionscript regular expression for repeating characters

  17. 17

    PHP Regular Expression special characters

  18. 18

    Regular expression get last characters

  19. 19

    Regular expression that checks if characters are escaped

  20. 20

    Special Characters in JFlex Regular Expression

  21. 21

    regular expression match digit and characters

  22. 22

    Regular expression for special characters and numericals

  23. 23

    Regular expression that ignores certain characters

  24. 24

    replacing characters with javascript regular expression

  25. 25

    Is there a Regular Expression to find characters in sequence?

  26. 26

    Regular expression with drools (special characters /* )

  27. 27

    Regular expression for anything except ']]' characters

  28. 28

    Regular Expression exact count of characters

  29. 29

    Trying to escape period in JavaScript regular expression but failing

HotTag

Archive