Page 1 of 1
need regex for community builder
Posted: Tue May 04, 2010 7:59 am
by gato_de_botas
hi, I need a regex to use in Community Builder, with Joomla, to mask a field like this
XXX.XXX.XXX-XX (where X = numbers)
I tried many options suggested by many people, but none are working, I dont know why. See the options I tried:
^\d{3}\.\d{3}\.\d{3}-\d{2}$
\^\d{3}\.\d{3}\.\d{3}-\d{2}$\
/^\d{3}\.\d{3}\.\d{3}-\d{2}$/
and many other options I see in the Regex Library, at
http://regexlib.com/ but nothing is working.
What regex I should use, please write exactly in the way I will have to write.
thanks!
Re: need regex for community builder
Posted: Tue May 04, 2010 8:05 am
by gato_de_botas
Here another expressions I tried, from Regex Library:
(^\d{3}\x2E\d{3}\x2E\d{3}\x2D\d{2}$)
(\d{3}.?\d{3}.?\d{3}-?\d{2})
(^(\d{3}.\d{3}.\d{3}-\d{2})|(\d{11})$)
(^(\d{2}.\d{3}.\d{3}/\d{4}-\d{2})|(\d{14})$)|(^(\d{3}.\d{3}.\d{3}-\d{2})|(\d{11})$)
None of them is working, and I am in doubt about using ( and ), and also / and \ at the beginning and end of the expressions. Seems that Community Builder needs a delimiter, but I dont know which to use.
Help please!
thanks
Re: need regex for community builder
Posted: Tue May 04, 2010 8:41 am
by ridgerunner
You didn't describe the format of your typical data. i.e. Do you have a long string with this pattern embedded in it? Or does the whole string consist of just the one field to be matched?
Try one of these:
Code: Select all
// Case 1. The target to be matched is the whole string. (Use ^ and $ anchors)
if (preg_match('/^\d{3}\.\d{3}\.\d{3}-\d{2}$/m', $contents)) {
# Successful match
} else {
# Match attempt failed
}
// Case 2. The target to be matched is embedded in a larger string. (Use \b anchors)
if (preg_match('/\b\d{3}\.\d{3}\.\d{3}-\d{2}\b/m', $contents)) {
# Successful match
} else {
# Match attempt failed
}
If you are going to work with regular expressions, you'll need to spend a little time to learn the proper syntax required by the PHP preg_* functions. You need to first take your regex say: '\d{3}' and wrap it in delimiters (which can be any non alpha-numeric), like so '%\d{3}%'. Choose a delimiter that is not in the regex pattern (the most common choice is '/' - I have used '%' here). Then you place any modifiers you need after the closing delimiter (such as 'i' for "ignore-case mode", 'm' for "multi-line mode", etc.) Then wrap the whole pattern in a single quoted string. This is then passed to the preg_* function like so: preg_replace('%\d{3}%im', 'replacetext');
Hope this helps!

Re: need regex for community builder
Posted: Tue May 04, 2010 9:17 am
by gato_de_botas
thanks runner,
The number is a CPF - a document format used in Brazil, like 444.555.666-77
I have only a field to enter the expression, see this image, I have to enter the expression in the Perl Regular Expression, just one line of text:

I am confused, please write exatly in the way I have to write in the field, without any other caractheres like ' or (, use only what I have to write.
Re: need regex for community builder
Posted: Tue May 04, 2010 9:29 am
by gato_de_botas
runner,
I found this expression in a php file pointed by the error message, give a look:
$validated = preg_match( $pregExp, $value );
if ( ! $validated ) {
$pregExpError = $field->params->get( 'pregexperror', CBTxt::T('Not a valid input') );
$this->_setValidationError( $field, $user, $reason, $pregExpError );
}
}
}
return $validated;
}
}
Re: need regex for community builder
Posted: Wed May 05, 2010 9:17 am
by ridgerunner
Ok, so you are NOT writing PHP code yourself. This helps. Try this one first:
[text]^\d{3}\.\d{3}\.\d{3}-\d{2}$[/text]
Re: need regex for community builder
Posted: Wed May 05, 2010 10:23 am
by gato_de_botas
thanks again, Runner
I got an error message "NOT A VALID INPUT" and "Warning: preg_match() [function.preg-match]: No ending delimiter '^' found in C:\wamp\www\joomla\components\com_comprofiler\plugin\user\plug_cbcore\cb.core.php on line 74"
I tried adding the slashe \ at the beginning and end of the expression, but got the same result.
What do you think?
Re: need regex for community builder
Posted: Wed May 05, 2010 9:21 pm
by ridgerunner
Ok that info helps. The community builder code requires that you supply the Perl regular expression with regex delimiters. Good. Try this one next:
[text]/^\d{3}\.\d{3}\.\d{3}-\d{2}$/[/text]
Re: need regex for community builder
Posted: Thu May 06, 2010 7:09 am
by gato_de_botas
thanks Runner,
this time only the message "Not a valid input".
I entered the expression
/^\d{3}\.\d{3}\.\d{3}-\d{2}$/
and tried the numbers 222.333.444-55
Re: need regex for community builder
Posted: Thu May 06, 2010 11:49 am
by ridgerunner
I'm not sure what to say. The regex provided is the correct one to match your data pattern but the tool you are using evidently does not like it.
I have no familiarity with this "Community builder" software and don't have time to investigate what might be causing the problem. However, you may want to experiment with the Perl regex option using some simpler data and matching patterns...
Good luck - rr