JaeWebb
asked on
How Do I Enforce Specific String on the Regular Expression Test?
I two regular expressions to check a single string. The string has characters that my programming language, ActionScript 3, uses as keywords (e.g. 'true', '$'). These are examples of the strings I am expecting to run tests on:
true
true,$
true, $
The first regular expression follows these rules:
1. must begin with the string "true"
2. may or may not have a comma after "true"
3. may have a string of any length after comma (I'll only use one character, the '$', after comma right now - that may change in future)
The expression I have so far is this: /[\test]/
If the first expression evaluates as true, the second regular expression will test the same string with the following rules:
1. must have a dollar sign ($) after the comma (I may swap out the dollar sign for another symbol in the future)
2. may or may not have a space after the comma
The expression I have so far is this: /[\test,($| $)]/
Right now, the expression still returns true if I add extra spaces or commas before the dollar sign and when I leave the comma out. I need the regular expression to follow the rules exactly so that if someone leaves out the comma, etc., the expression will return false.
Help! I need an answer quickly...
true
true,$
true, $
The first regular expression follows these rules:
1. must begin with the string "true"
2. may or may not have a comma after "true"
3. may have a string of any length after comma (I'll only use one character, the '$', after comma right now - that may change in future)
The expression I have so far is this: /[\test]/
If the first expression evaluates as true, the second regular expression will test the same string with the following rules:
1. must have a dollar sign ($) after the comma (I may swap out the dollar sign for another symbol in the future)
2. may or may not have a space after the comma
The expression I have so far is this: /[\test,($| $)]/
Right now, the expression still returns true if I add extra spaces or commas before the dollar sign and when I leave the comma out. I need the regular expression to follow the rules exactly so that if someone leaves out the comma, etc., the expression will return false.
Help! I need an answer quickly...
^true(?:,[\S\s]*)?$
ASKER
Thanks for the rapid response. Would you mind separating that into two regular expressions so I can use it in two different tests? Separating it will also help me to understand it more clearly. For example:
if test 1 is true
do this
do this
if test 2 is true
do this too
if test 1 is true
do this
do this
if test 2 is true
do this too
It's best to keep it as one test, otherwise the string may not be formatted correctly. Here is the description for the pattern:
----------------------------------------------------------------------
^ the beginning of the string
----------------------------------------------------------------------
true 'true'
----------------------------------------------------------------------
(?: group, but do not capture (optional
(matching the most amount possible)):
----------------------------------------------------------------------
, ','
----------------------------------------------------------------------
[\S\s]* any character of: non-whitespace (all
but \n, \r, \t, \f, and " "), whitespace
(\n, \r, \t, \f, and " ") (0 or more
times (matching the most amount
possible))
----------------------------------------------------------------------
)? end of grouping
----------------------------------------------------------------------
$ before an optional \n, and the end of the
string
----------------------------------------------------------------------
ASKER
I attached a segment of the funtion I'm writing so you can see why I need two separate tests.
I'm using a user-supplied string to test for a numeric value. I agree that I should be using your regular expression for the first test, 'isTrue'. The problem is that the value could be true AND it could specify that the number should be used as currency. In some cases, the value could be numeric without symbolizing money; I still need to display the number without displaying it as currency. See below:
-------------------------- ---------- ---------- ---------- ------
var isTrue:RegExp =/^true(?:,[\S\s]*)?$/ ;
var isCurrency:RegExp = /^true(?:,[\S\s]*)?$/;
if ( isTrue.test( qualifier.toLowerCase() ) )
{
_adm.sortFieldsArray.push( new SortField( dgc.dataField, false, false, true ) );
dgc.minWidth = 32;
//uses new test value
var testString:String = "true";
trace( "The qualifier is: " + qualifier.toLowerCase() + ", the test value is: " + testString + ", the test returns: " + isCurrency.test( testString ).toString() );
if ( isCurrency.test( testString ) )
{
dgc.labelFunction = displayCurrencyFormat;
}
}
-------------------------- ---------- ---------- ---------- ------
as you can see, isCurrency needs to be a separate test.
I'm using a user-supplied string to test for a numeric value. I agree that I should be using your regular expression for the first test, 'isTrue'. The problem is that the value could be true AND it could specify that the number should be used as currency. In some cases, the value could be numeric without symbolizing money; I still need to display the number without displaying it as currency. See below:
--------------------------
var isTrue:RegExp =/^true(?:,[\S\s]*)?$/ ;
var isCurrency:RegExp = /^true(?:,[\S\s]*)?$/;
if ( isTrue.test( qualifier.toLowerCase() ) )
{
_adm.sortFieldsArray.push(
dgc.minWidth = 32;
//uses new test value
var testString:String = "true";
trace( "The qualifier is: " + qualifier.toLowerCase() + ", the test value is: " + testString + ", the test returns: " + isCurrency.test( testString ).toString() );
if ( isCurrency.test( testString ) )
{
dgc.labelFunction = displayCurrencyFormat;
}
}
--------------------------
as you can see, isCurrency needs to be a separate test.
var isTrue:RegExp =/^true(?:,[\S\s]*)?$/ ;
var isCurrency:RegExp = /^true(?:,[\S\s]*)?$/;
if ( isTrue.test( qualifier.toLowerCase() ) )
{
var testString:String = "true";
trace( "The qualifier is: " + qualifier.toLowerCase() + ", the test value is: " + testString + ", the test returns: " + isCurrency.test( testString ).toString() );
_adm.sortFieldsArray.push( new SortField( dgc.dataField, false, false, true ) );
dgc.minWidth = 32;
if ( isCurrency.test( testString ) )
{
dgc.labelFunction = displayCurrencyFormat;
}
}
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
ASKER
I should also add that the '$' symbol is meant to be a formatting-type identifier. The isCurrency test should on the '$' being present. No '$' symbol, no currency format. In the future, I may add some other format, in which case I'd use a different symbol.
This regular expression: ^true(?:,[\S\s]*)?$
...evaluates strings like 'true,Y" and "true,3" as true when they should evaluate as false.
This regular expression: ^true(?:,[\S\s]*)?$
...evaluates strings like 'true,Y" and "true,3" as true when they should evaluate as false.
<html>
<body>
<script type="text/javascript">
var re = /^true(, ?\$.*)?$/g;
var str = "true, $100";
while((resultArray = re.exec(str)) != null)
{
alert("Test 1 successful");
if ((resultArray[1]) != "")
{
alert("Test 2 successful");
}
}
</script>
</body>
</html>
ASKER
I see that it works in JavaScript; I ran your code in an HTML page. When I use the regular expressions as follows:
var isTrue:RegExp =/^true\b(.*)$/;
var isCurrency:RegExp = /^, ?\$/g;
The trace statement in my ActionScript code above reports as follows:
The qualifier is: true, the test value is: true, $, the test returns: false
My code will encounter the test string "true,$" from a spreadsheet table cell. The spreadsheet users will specify if a column should be treated as a number or text ('true') and also if it should be formatted as currency ('true,$' or 'true, $' if they habitually add a space after the comma).
var isTrue:RegExp =/^true\b(.*)$/;
var isCurrency:RegExp = /^, ?\$/g;
The trace statement in my ActionScript code above reports as follows:
The qualifier is: true, the test value is: true, $, the test returns: false
My code will encounter the test string "true,$" from a spreadsheet table cell. The spreadsheet users will specify if a column should be treated as a number or text ('true') and also if it should be formatted as currency ('true,$' or 'true, $' if they habitually add a space after the comma).
ASKER
I tried the single test too:
var re = /^true(, ?\$.*)?$/g;
I don't have a solution yet... I still need an 'isCurrency' regular expression (something that verifies a value as one of these two strings only:
test,$
test, $
Please see the comments in code snippet below.
var re = /^true(, ?\$.*)?$/g;
I don't have a solution yet... I still need an 'isCurrency' regular expression (something that verifies a value as one of these two strings only:
test,$
test, $
Please see the comments in code snippet below.
var isTrue:RegExp =/^true\b(.*)$/;
var isCurrency:RegExp = /^, ?\$/g;
//The function 'qualifier.toLowerCase()' represents data that is loaded into the application and
//is always equal to "true" for this test.
if ( isTrue.test( qualifier.toLowerCase() ) )
{
_adm.sortFieldsArray.push( new SortField( dgc.dataField, false, false, true ) );
dgc.minWidth = 32;
//I am changing the value between the quotes during each test.
//Nothing but ",$" or ", $" should return as true in the testString value
var testString:String = qualifier.toLowerCase() + ",$";
trace( "The qualifier is: " + qualifier.toLowerCase() + ", the test value is: " + testString + ", the test returns: " + isCurrency.test( testString ).toString() );
if ( isCurrency.test( testString ) )
{
dgc.labelFunction = displayCurrencyFormat;
}
}
I can't help much more than what I have, I cannot test on your platform.
ASKER
Your regular expressions work in JavaScript, which is one of the languages I specified in my original post, so I'll give you the points. I'm going to repost this problem with a different objective: "Help me get this regular JavaScript expression to work in ActionScript 3"!
When I repost, I'll use the code sippet appearing below.
When I repost, I'll use the code sippet appearing below.
<html>
<head>
<style type="text/css">
#doc {background-color:#EFEFEF;text-align:center;}
#testBlock {background-color:#FFFFFF;text-align:left;width:600px;height:500px;margin: 0 auto;border:1px solid #FF9900;padding:0 20px;}
.regexp {color:#FF0000;}
.testval {color:#00CC00;}
</style>
</head>
<body id="doc">
<div id="testBlock">
<h1 id="test1"></h1>
<h1 id="test2"></h1>
<h1 id="testValue"></h1>
<p>The only two true values should be "true,$" and "true, $"; everything else should be false.</p>
<script type="text/javascript">
//TEST YOUR REGULAR EXPRESSIONS HERE.
var re1 = /^true\b(.*)$/;
var re2 = /^, ?\$/;
var str = "true,$";
//THE VALUES USED IN YOUR TESTS WILL APPEAR IN THE PAGE...
document.getElementById( "test1" ).innerHTML = "Regular Expression 1: <span class='regexp'>" + re1 + "</span>";
document.getElementById( "test2" ).innerHTML = "Regular Expression 1: <span class='regexp'>" + re2 + "</span>";
document.getElementById( "testValue" ).innerHTML = "Test Value: <span class='testval'>" + str + "</span>";
if ( ( resultArray = re1.exec( str ) ) != null )
{
alert( "Test 1 successful" );
if ( re2.test( resultArray[ 1 ] ) )
{
alert( "Test 2 successful" );
}
else
{
alert( "Test 2 failed" );
}
}
</script>
</div>
</body>
</html>
ASKER
I gave a 'B' because I still need additional help getting it to work in ActionScript 3.