Got more questions? Find advice on: ASP | SQL | XML | Windows
in Search
Welcome to RegexAdvice Sign in | Join | Help

Select Particular String Matches From A Given String

Last post 11-03-2009, 5:58 PM by Aussie Susan. 1 replies.
Sort Posts: Previous Next
  •  11-02-2009, 12:18 PM 57098

    Select Particular String Matches From A Given String

    Hi,

    I am using C#/.NET 3.5 to write a program that could identify particular ONE or MORE string matches in the given string and I need help in this. :-)

    Example String:

    SELECT * FROM Users WHERE FIRST = ?Param1? AND LAST = ?paraMETer2? HAVING MONTHLY_Salary

    > ?parameterforsalary?

     Requirement:

    I want a list or a string array that contains Param1, paraMETer2 and parameterforsalary from above string.

    Explanation: 

    I have a method that will return a string like the above shown in example. This string will contain one or more Parameters opening and closing ? (question marks). Now, I want to be able to identify all those parameters to do further actions.

    Also, there could be whitespaces in the given string, mulitple lines etc.. (I am not sure what to do if the user forgets to type in the closing question mark for a particular parameter, probably it will then take all the rest of string as just one parameter... ). Inside ?param defintion? can be any word, digital characters etc.

     I've tried few simple regex expressions but since I'ven't worked on it much, I couldn't figure it out.

     Any help is greatly appreciated.

    AQ78

  •  11-03-2009, 5:58 PM 57128 in reply to 57098

    Re: Select Particular String Matches From A Given String

    Based on the limited example you have provided:

    \?.*?\?

    will locate the text between 2 question marks, but be warned: there are MANY ways in which this can be broken, not the least of which is if a "?" appears somewhere else in the text. Something like:

    select * from users Where first = ? and Last = ?param?;

    will throw it into a spin.

    If you can be certain that there are never any question marks (even in comments etc.) in the text, then the above will do. If not then you will need to be more specific as to how we can determine where the valid values can occur or what they can contain to try to limit what is matched.

    Susan

View as RSS news feed in XML