0
/^[a-z][ ][=][>][ ][a-z?][.?][a-z0-9]+[ ][=][ ]['?][a-z0-9]+['?]]/i  

I'm trying to figure out how to get a rexex pattern that would recognize a string of lambda syntax (used in c#)

In the case of strings

"p => p = 'some random string'" //Must alow for single quotes  

In the case of number or boolean values

"p => p = true" /*or*/ "p => p = 25"  //Must allow for a string without single quotes with no whitespace at all in the event there are no single quotes  

Also it must allow for a single '.' in the letter chosen to the left of the '=' sign

"p => p.firstName = 'Jack'"  

How can I modify my regex to fulfill the following requirments

  1. start off with any letter
  2. followed with a mandatory space
  3. followed by a mandatory string '=>' (without single quotes)
  4. followed by a mandatory space
  5. followed by the same letter in the step 1 (or at least a single character)
  6. followed by a period character (optional)
  7. followed by any set of alphanumberic characters (required if there is a period from step 6)
  8. followed by a space
  9. followed by an equals sign
  10. followed by a space
  11. followed by any alphanumeric set of characters along with single quotes (but only if the single quotes encompass the set of alphanumeric characters)
2
  • static bool greaterThanTwo(int arg) is a predicate. Do you mean lambda predicates: List<int> newList = list.FindAll(i => i > 2); Commented Apr 3, 2016 at 23:01
  • My mistake. Corrected it. Commented Apr 3, 2016 at 23:10

1 Answer 1

1

First off, just the general point that you don't need [] around everything, only character classes (e.g [a-zA-Z] or [_\$0-9]).

So let's go through your steps in order:

  • Match any letter - you don't specify case, let's do both:
    • Lowercase only: ([a-z])
    • Uppercase only: ([A-Z])
    • Both: ([a-zA-Z]).
    • We wrap it in () so we can use it in a backreference later.
  • The mandatory string => (merging steps 2-4) is just that, literally: =>. As none of these are special characters there is no need for escaping.
  • To get the same letter as step 1, we insert a backref to the first group (set of ()): \1
  • For step 6 & 7, we take the period along with one alphanumeric character to be optional: (\.\w)? and then zero or more alphanumeric characters: \w*
  • Now we have the literal string =, again none of these chars need to be escaped so we include it directly: =
  • For the last step we have several options:
    • Some numeric characters without whitespace: \d+
    • True or False
    • Or, single quote, any characters but the single quote and then single quote again: '[^']*' (we use negative character classes to get everything but ')
    • Now we join these to together as alternatives using |

Putting all this together, we get the final regex:

/([a-zA-Z]) => \1(\.\w)?\w* = (\d+|true|false|'[^']*')/i

Sign up to request clarification or add additional context in comments.

6 Comments

Thanks Tuomas. This is almost getting me there. This doesn't match the ending single quote and in the case we don't use single quotes it matches alpha characters other than true or false. I will mark you as correct but I want to try and figure out how to alter your regex to make it 100% correct
I caught the mistake in your final regex. You just forgot to add that ' after the *.
@Adrian fixed that, sorry it was a typo.
I modified it to accept decimals as well /([a-zA-Z]) => \1(\.\w)?\w* = (\d+[.]?\d+|true|false|'[^']*')/i
@Adrian That [.]? can just be \.?. Unless you like to put them in char classes?
|

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.