How NOT to match in just one regex

Zerquix18

I'm trying to validate a query with preg_match. I want to replace all %s, %d and %f but NOT with \%s, \%d and \%f. My regex is:

$query = "UPDATE FROM lol SET lol = \%s, asd = %s WHERE lol = %d";
$reg = preg_match("(\%s|\%d|\%f)(?!\\\%s|\\\%d|\\\%f)", $query, $matches);
var_dump($matches); // %s and %d, because \%s can't be matched

I'm trying to not match with \%s, because if you do something like: "WHERE st LIKE %s" (starting with s) it will crash. Then I want to validate it with a regex and after replacing to remove the slashes. I will use the matches %s, %d and %f to replace it like str, int and float, so I want to use just one regex. Can you help me?

zx81

Zerquix, here's the most compact regex I could think of. It only matches the right ones.

$regex = "~\\\\%(*SKIP)(*F)|%[sdf]~";
$string = "match %s, %d and %f but NOT with \%s, \%d and \%f.";
if(preg_match_all($regex,$string,$m)) print_r($m);

See live demo

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to match any of these words, but just the last one in the list with .NET Regex

From Dev

How to match just the first number in Regex

From Dev

How to match just 1 or 2 chars with regex

From Dev

Match just one word among specific others with regex

From Dev

Match just once with regex

From Dev

How do I get my regex to return just my match?

From Dev

How to just return two numbers after regex match with grep or sed?

From Dev

How to get only one match in this regex?

From Dev

How to match one or more characters in a string with regex?

From Dev

How to match one per line in a regex?

From Dev

Make Regex Match Just Once

From Dev

How to append just one line after found match with sed?

From Dev

How to find and match just one element list in the java stream?

From Dev

How to append just one line after found match with sed?

From Dev

How to match "one sequence of characters to an other one, that can repeat" regex?

From Dev

Multiple replacement with just one regex

From Dev

Regex help to match just the url in log

From Dev

Python: how best to combine two regex's into one pattern match?

From Java

how to match exactly one word among the others in REGEX 'or'(|) condition

From Dev

How do use regex to find match multiple matches for one attribute?

From Dev

Regex, how do I say, match everything but one word?

From Dev

How to Choose the Preferred (but Specific) Regex Match Over a Default (but Universal) One?

From Dev

How to match a regular expression with exactly one digit in it using python regex?

From Dev

How to match one of multiple alternative patterns with C++11 regex

From Dev

in regex how to match multiple Or conditions but exclude one condition

From Dev

Java Matcher: How to match multiple lines with one regex

From Dev

in regex how could one match quotes and dots for c# programming

From Dev

How do use regex to find match multiple matches for one attribute?

From Dev

How can I get one match with RegEx from start of string

Related Related

  1. 1

    How to match any of these words, but just the last one in the list with .NET Regex

  2. 2

    How to match just the first number in Regex

  3. 3

    How to match just 1 or 2 chars with regex

  4. 4

    Match just one word among specific others with regex

  5. 5

    Match just once with regex

  6. 6

    How do I get my regex to return just my match?

  7. 7

    How to just return two numbers after regex match with grep or sed?

  8. 8

    How to get only one match in this regex?

  9. 9

    How to match one or more characters in a string with regex?

  10. 10

    How to match one per line in a regex?

  11. 11

    Make Regex Match Just Once

  12. 12

    How to append just one line after found match with sed?

  13. 13

    How to find and match just one element list in the java stream?

  14. 14

    How to append just one line after found match with sed?

  15. 15

    How to match "one sequence of characters to an other one, that can repeat" regex?

  16. 16

    Multiple replacement with just one regex

  17. 17

    Regex help to match just the url in log

  18. 18

    Python: how best to combine two regex's into one pattern match?

  19. 19

    how to match exactly one word among the others in REGEX 'or'(|) condition

  20. 20

    How do use regex to find match multiple matches for one attribute?

  21. 21

    Regex, how do I say, match everything but one word?

  22. 22

    How to Choose the Preferred (but Specific) Regex Match Over a Default (but Universal) One?

  23. 23

    How to match a regular expression with exactly one digit in it using python regex?

  24. 24

    How to match one of multiple alternative patterns with C++11 regex

  25. 25

    in regex how to match multiple Or conditions but exclude one condition

  26. 26

    Java Matcher: How to match multiple lines with one regex

  27. 27

    in regex how could one match quotes and dots for c# programming

  28. 28

    How do use regex to find match multiple matches for one attribute?

  29. 29

    How can I get one match with RegEx from start of string

HotTag

Archive