I’m completely new to regular expressions, and I need to filter all the words of at least 3 characters (and a maximum size of 16) out of a text. (so I can enter those data into a MySQL database)
Currently, everything works, except for the regular expression:
/^.{3,16}$/
(I constructed this from a tutorial found using Google 😉 )
Thanks!
Yvan
Sample Data:
rjm1986 * SinuhePalma * excel2010 * Jimineedles * 209663603 * C6A7XR * Snojog * XmafiaX * Cival2 * HitmanPirrie * MAX * 4163016 * Dredd23 * Daddy420 * mattpauley * Mykillurdeath * 244833585 * KCKnight * Greystoke * Fatbastard * Fucku4 * Davkar * Banchy2 * ET187 * Slayr69 * Nik1236 * SeriousAl * 315791 * 216996334 * K1ra * Koops1 * LastFallout * zmileben * bismark * Krlssi * FuckOff1 * 1owni * Ulme * Rxtvjq * halfdeadman * Jamacola * LBTG1008 * toypark * Magicman6497 * Tyboe187 * Bob187 * Zetrox
PHP Code (yeah, I know – it’s kind of sloppy – this is only used to generate the queries…)
<?php
//regexer.php
$text = @$_REQUEST['fText'];
if ($text == '') {
?>
<form method="post" action="">
<input type="text" name="regex" />
<textarea name="fText"></textarea>
<br />
<input type="submit"></input>
</form>
<?php
} else {
preg_match_all($_REQUEST['regex'], $_REQUEST['fText'], $matches);
header ("Content-type: text/plain");
foreach ($matches as $match) {
//print_r($match);
echo ("INSERT INTO maf_codes (Code, GameID) VALUES ('$match', %GAMEID%);\n");
}
}
?>
Found a solution: replace the $_REQUEST[‘regex’] with the regex did work 😉
Try this:
Explained:
\bmatches a word boundary\wmatches a word character{3,16}applies to the\wand it indicates that at least 3 and at most 16 characters should be matched.FYI: I omitted the start anchor (
^) and end anchor ($) from the regex you noted in your question because it seems like you want to find matches with longer strings of text as input and the anchors would restrict the matching to only instances where the entire input string matched.UPDATE:
Here is the proof that this regex works:
Outputs: