0

How can i found word in string using regex or not in this string

String is Mysql Query

SELECT 
  id,
  name,
  desc,
  (SELECT id as lid FROM comments WHERE lid = id order by id desc limit 1) as lid_com
 FROM posts limit 3

here i want to search for limit in the last of string

string may be

  limit 3
  limit 3, 3
  limit 3 , 3
  limit 3 ,3

3 here may be any number

i tried this regex but i'm beginner

"/ limit [0-9]{0,9}+\,[0-9]{0,9} /i$"

how can i do this

Thank you

3
  • may be query is not vaild mysql query but i added it with subquery wth limit word , heres query contain 2 words of limit ^-^ Commented Apr 9, 2012 at 12:45
  • Would this question : "How to find the first word of a string" fit better to what you are looking for ? Commented Apr 9, 2012 at 12:46
  • @FailedDev yes i want to find it in the main query not in the subquery Commented Apr 9, 2012 at 12:48

4 Answers 4

1
$string = 'SELECT
  id,
  name,
  desc,
  (SELECT id as lid FROM comments WHERE lid = id order by id desc limit 1) as lid_com
 FROM posts limit 3 ,3';

echo preg_match('/(limit)\s\d(((\,\s)|(\s\,\s)|(\s\,))\d)?$/i', $string, $matches);  //1
print_r($matches); //$matches['0'] == 'limit 3 ,3'
Sign up to request clarification or add additional context in comments.

4 Comments

Thank you @lng , your regex work fine , but not with this limit 3,3
Now its work fine after regex become /(limit)\s\d(((\,\s)|(\s\,\s)|(\s\,))\d)?$/i , but as @kirilloid said limit is not always last statment dev.mysql.com/doc/refman/5.0/en/select.html , this will need some more work :)
Just take out the $ so the regex will match anywhere in the string, not just the end :)
This Will affect sub query if found , i want the main query
1
"/limit[\s]+[\d]+[\s]*,[\s]*[\d]+$/i"

This searches case insesitive for:

  • "limit"
  • one or more whitespaces
  • one or more digits
  • zero or more whitespaces
  • a comma
  • zero or more whitespaces
  • one or more digits
  • at the end of the string

2 Comments

maybe add ' posts ' before 'limit' to only get the second occurrence
not work ( without comment on your answer ) , and i will not control the query :(
1

Do not remember, whether LIMIT is always a last SQL statement, so I'd use

/limit\s+(\d+)(?:\s*,\s*(\d+))?(?=[^\n]+\Z)/mi

3 Comments

Thank you for answer . but this regex work with the subquery too ?? i want to check it in the main query only not sub queries
It matches only those limit statement, which is located on the last line. Parsing arbitrary SQL code with regexes isn't good idea.
Find some SQL code parser and integrate it =) But actually your underlying problem may be solved w/o replacing at all, just try to solve it from another side.
0

Try this condition -

SELECT * FROM table WHERE column
  REGEXP '(^limit[[:space:]]+[[:digit:]]+[[:space:]]*,[[:space:]]*[[:digit:]]+$)|(^limit[[:space:]]+[[:digit:]]+[[:space:]]*$)';

The first part find strings like 'limit 3,3' -

^limit[[:space:]]+[[:digit:]]+[[:space:]]*,[[:space:]]*[[:digit:]]+$

The second one finds strings like 'limit 3' -

^limit[[:space:]]+[[:digit:]]+[[:space:]]*$

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.