Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

regular expression help

Options
  • 21-07-2008 4:16pm
    #1
    Closed Accounts Posts: 39


    Hi, im trying to create a regular expression to validate user input on a web site. i want to check for the '$' character in the input string and validate what comes after each occurrence of '$'.

    e.g. if my user is input is something like:
    "some text here $validstring1 more text $validstring2 some more text $invalidstring"

    i want a regular expression to tell me that this is invalid input as the $invalidstring part is invalid. There is a pre-defined set of about 20 strings that can occur after the '$' character. The user's input string could potentially contain none or all of these strings.


Comments

  • Registered Users Posts: 75 ✭✭conorod


    Questions:
    1. What programming language are you using?
    2. Do you know how many strings the user will enter?


  • Closed Accounts Posts: 39 oochie


    1. its a Java web app but the regular expression is in a commons validator config file. But i guess its processed by some Java class or other. I want to try validate the input automatically using a regular expression & without having to examine the string in my Java code.
    My config file has entries like this to validate input for an email address:
    <var-value>
    	<![CDATA[^[a-zA-Z0-9.]+@mydomain.com$ ]]>
    </var-value>
    

    2. No i dont know how many strings the user will enter. It will be a paragraph of free text.


  • Registered Users Posts: 75 ✭✭conorod


    I'll think about it, I'm not the best with Java and regex, sorry. Hopefully someone else can figure it out if I can't.


  • Registered Users Posts: 1,393 ✭✭✭Inspector Gadget


    I'm going to go ahead and assume that whatever's applying the regex to the string will recursively match a pattern against every hit it finds in the string, but assuming that's so, something simple like the following should do:
    \$(\w+)
    

    That will match a dollar sign followed by any combination of A-Z (upper or lower case), 0-9, and underscores (_); for example $test, or $test_1, but will strip out the dollar sign when returning it. If you want to do this with a search and replace in mind, you'll have to match the dollar sign too, so you could try this:
    (\$(?:\w+))
    

    I tried these in jEdit's search and replace thingy, which as far as I remember uses java.util.regex, and they worked fine - sorry, not a Java head. It'll work fine in Perl, PHP or Python, or anything else using PCRE (i.e. Perl-compatible regex) though. Here's what Python says, for example:
    Python 2.5 (r25:51918, Sep 19 2006, 08:49:13) 
    [GCC 4.0.1 (Apple Computer, Inc. build 5341)] on darwin
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import re
    >>> teststring = "some text here $validstring1 more text $validstring2 some more text $invalidstring"
    >>> print re.findall("\$(\w+)", teststring)
    ['validstring1', 'validstring2', 'invalidstring']
    >>> print re.findall("(\$(?:\w+))", teststring)
    ['$validstring1', '$validstring2', '$invalidstring']
    >>> 
    

    That's what you want, right?
    Gadget


  • Registered Users Posts: 21,264 ✭✭✭✭Hobbes


    Using Inspector Gadgets regex expression and RegExBuddy I asked it for the code to to move through the matches.
    try {
    	Pattern regex = Pattern.compile("\\$(\\w+)", Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE | Pattern.MULTILINE);
    	Matcher regexMatcher = regex.matcher(subjectString);
    	while (regexMatcher.find()) {
    		// matched text: regexMatcher.group()
    		// match start: regexMatcher.start()
    		// match end: regexMatcher.end()
    	} 
    } catch (PatternSyntaxException ex) {
    	// Syntax error in the regular expression
    }
    

    This would probably be the best way. Then have your string of valid entries in one string and try to match your result. For example.
    String t = "$one$two$three";
    String r = "$two";
    
    if ( t.contains(r)) System.out.println("Found it");
    


  • Advertisement
Advertisement