Destramic Posted April 26, 2011 Share Posted April 26, 2011 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 More sharing options...
requinix Posted April 26, 2011 Share Posted April 26, 2011 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 More sharing options...
Destramic Posted April 26, 2011 Author Share Posted April 26, 2011 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 More sharing options...
Destramic Posted April 26, 2011 Author Share Posted April 26, 2011 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)) anyone? Link to comment https://forums.phpfreaks.com/topic/234718-expression-help/#findComment-1206511 Share on other sites More sharing options...
.josh Posted April 26, 2011 Share Posted April 26, 2011 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 More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.