0

I'm trying to find a regex that will match a specific expression in the following format:

name = value

However, I need it to not match:

name.extra = value

I have the following regex:

([\w\@\-]+) *(\=|\>|\>\=|\<|\<\=) *([^\s\']+)

which matches the first expression, but also matches the second expression (extra = value).

I need a regex that will match only the first expression and not the second (i.e. with a dot).

2
  • Use a negative lookbehind assertion. Commented Dec 28, 2012 at 4:00
  • It seems that you are trying to grab something from code. Technically, it wouldn't be invalid to be able to write something like name . extra (spaces in between), but this is so rare that I haven't seen such bad code myself. If you can assume there would be no such case, then it can be solved easily with negative look-behind. Commented Dec 28, 2012 at 4:04

3 Answers 3

1

Just add ^ beginning and $ ending to your expression

^([\w\@\-]+) *(\=|\>|\>\=|\<|\<\=) *([^\s\']+)$
Sign up to request clarification or add additional context in comments.

3 Comments

I dont know why this answer got negative votes. But technically it is correct. Check This
@om39a Because it's presumptuous to assume OP only wanted to match a single line, nor whitespace on either end, or disregard more complex expressions x = (name = y);.
@mario I agree with you.. But I am Curious to know how it can be handled. By using negative look behind and starting of the word? Like this (?<!.)\b
0

Negative lookbehind assertion (?<!) might be what you are looking for.

For a simple assignment: (?<!\.)\b(\w+)\s*=\s*(\w+)

summary:

  • (?<!\.) = prevent the character . at that location
  • \b = beginning of a word

The captured words are:

  • \1 = destination name
  • \2 = source name

and using the regex you specified, this should give something near this:

(?<!\.)\b([\w\@\-]+) *(\=|\>|\>\=|\<|\<\=) *([^\s\']+)

6 Comments

You should at least try to mirror whatever the OP has worked out.
But the above regex will fail for "name <= value" or "name >= value"
fixed. I forgot the spaces in the last regex, still the idea is the same as in the first assignment regex.
@DoesntMatter Thanks. I was trying this in RegexBuddy but I noticed some issues. When I use this: (?<!.)([\w\@\-]+) *(\=|\>|\>\=|\<|\<\=) *([^\s\']+) it works but it doesn't match when I have a space before the expression e.g. ` name = value` will not match. Can you fix this?!
Ok, I changed it to this: (?<!\.)\b([\w\@\-]+) *(\=|\>|\>\=|\<|\<\=) *([^\s\']+) and it works ok now. Cheers.
|
0

You don't say what language you're using, but it sounds like you don't need to use regexes at all.

If you're using PHP, then use the explode function to break apart on the =. Then check to see if the argument name has a period in it.

1 Comment

I have PHP and Regex Tags on the question. Also, trust me when I say I know what I need, and I need Regex.

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.