Jump to content

Recommended Posts

im in need of some help on a bit of regex...im trying to learn but my effort isnt work so i need some help please

 

the problem im having is this part of the scipt

 

if (preg_match('/^[A-Za-z0-9_]\s=\s[A-Za-z0-9_]$/', $i))

 

i want to beable to check the value is in the format of:

 

test_123=test123_123 (only letter number and underscores)

 

if anyone would could help me or give me some points on my regex i'd be very greatful thanks

 

	    while ($i = fgets($file))
    {
    	if (!preg_match('/^\s*$/', $i))
    	{
      		if (preg_match('/^[A-Za-z0-9_]\s=\s[A-Za-z0-9_]$/', $i))
      		{
      			preg_match('/^(.*?)=(.*?)$/', $i, $found);
      			
	      		$name                 = trim($found[1]);
	      		$value                = trim($found[2]);
	      		$this->_values[$name] = $value;
      		}
      	}
    }

Link to comment
https://forums.phpfreaks.com/topic/234718-expression-help/
Share on other sites

Your expression looks for, in order: the beginning of the string, one letter/number/underscore, a whitespace character, an equals sign, another whitespace character, another letter/number/underscore, and the end of the string.

Note how none of those words were plural...

 

There's no need to nest preg_matches. Just do it all at once.

read line
if (preg_match('/^\w+=\w+$/', line)) {
    line is good
}

By the way, unless you say something otherwise, I'll assume there's nothing more specific than "letters, numbers, and underscores" - no format, no restrictions, nothing but any combination of those characters.

Link to comment
https://forums.phpfreaks.com/topic/234718-expression-help/#findComment-1206180
Share on other sites

thank i had to remove ^ and $ from the expression to make it work

if (preg_match('/\w += +\w/', $i))

 

also im trying to match any strings in the format of

 

test[] = hello

test[] = bye

 

the expression below does work im just wondering if there is any faults with it as im new to this...thanks

 

else if (preg_match('/^^[A-Za-z0-9_][]$ += +[A-Za-z0-9_]/', $i))

Link to comment
https://forums.phpfreaks.com/topic/234718-expression-help/#findComment-1206338
Share on other sites

You need to escape a lot of that stuff, as they have special meaning in regex. For example, you have [A-Za-z0-9_] which you know is a character class to specify a range of characters.  Well [..] encloses and specifies that it is a character class.  But then you turn around and try to use [] to match a literal []...how does the engine know you now want to use those brackets to match literal brackets? Only way for it to know is if you escape the characters \[\]

 

You also have all kinds of stuff in there that just doesn't belong at all.  I suggest you go back to reading about what the symbols in a regex pattern actually do.

 

preg_match('/\w+\[\] = \w+/',$i)

 

or if you wanted to capture separate the parts:

 

preg_match('/(\w+\[\]) = (\w+)/',$i,$match)

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/234718-expression-help/#findComment-1206548
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.