Pattern matching regular expressions

I'm attempting to determine if a string matches a pattern of containing less than 100 alphanumeric characters a-z or 0-9 case insensitive. So my regular expression string looks like:
"^[a-zA-Z0-9]{0,100}$"And I use something like...
Pattern pattern = Pattern.compile( regexString );I'd like to modify my regex string to include the email 'at' symbol "@". So that the at symbol will be allowed. But my understanding of regex is very limited. How do I include an "or at symbol" in my regex expression?
Thanks for your help.

* Code by sabre150
private static final Pattern emailMatcher;
    static
        // Build up the regular expression according to RFC821
        // http://www.ietf.org/rfc/rfc0821.txt
        // <x> ::= any one of the 128 ASCII characters (no exceptions)
        String x_ = "\u0000-\u007f";
        // <special> ::= "<" | ">" | "(" | ")" | "[" | "]" | "\" | "."
        //              | "," | ";" | ":" | "@"  """ | the control
        //              characters (ASCII codes 0 through 31 inclusive and
        //              127)
        String special_ = "<>()\\[\\]\\\\\\.,;:@\"\u0000-\u001f\u007f";
        // <c> ::= any one of the 128 ASCII characters, but not any
        //             <special> or <SP>
        String c_ = "[" + x_ + "&&" + "[^" + special_ + "]&&[^ ]]";
        // <char> ::= <c> | "\" <x>
        String char_ = "(?:" + c_ + "|\\\\[" + x_ + "])";
        // <string> ::= <char> | <char> <string>
        String string_ = char_ + "+";
        // <dot-string> ::= <string> | <string> "." <dot-string>
        String dot_string_ = string_ + "(?:\\." + string_ + ")*";
        // <q> ::= any one of the 128 ASCII characters except <CR>,
        //               <LF>, quote ("), or backslash (\)
        String q_ = "["+x_+"$$[^\r\n\"\\\\]]";
        // <qtext> ::=  "\" <x> | "\" <x> <qtext> | <q> | <q> <qtext>
        String qtext_ = "(?:\\\\[" + x_ + "]|" + q_ + ")+";
        // <quoted-string> ::=  """ <qtext> """
        String quoted_string_ = "\"" + qtext_ + "\"";
        // <local-part> ::= <dot-string> | <quoted-string>
        String local_part_ = "(?:(?:" + dot_string_ + ")|(?:" + quoted_string_ + "))";
        // <a> ::= any one of the 52 alphabetic characters A through Z
        //              in upper case and a through z in lower case
        String a_ = "[a-zA-Z]";
        // <d> ::= any one of the ten digits 0 through 9
        String d_ = "[0-9]";
        // <let-dig> ::= <a> | <d>
        String let_dig_ = "[" + a_ + d_ + "]";
        // <let-dig-hyp> ::= <a> | <d> | "-"
        String let_dig_hyp_ = "[-" + a_ + d_ + "]";
        // <ldh-str> ::= <let-dig-hyp> | <let-dig-hyp> <ldh-str>
        // String ldh_str_ = let_dig_hyp_ + "+";
        // RFC821 looks wrong since the production "<name> ::= <a> <ldh-str> <let-dig>"
        // forces a name to have at least 3 characters and country codes such as
        // uk,ca etc would be illegal! I shall change this to make the
        // second term of <name> optional by make a zero length ldh-str allowable.
        String ldh_str_ = let_dig_hyp_ + "*";
        // <name> ::= <a> <ldh-str> <let-dig>
        String name_ = "(?:" + a_ + ldh_str_ + let_dig_ + ")";
        // <number> ::= <d> | <d> <number>
        String number_ = d_ + "+";
        // <snum> ::= one, two, or three digits representing a decimal
        //              integer value in the range 0 through 255
        String snum_ = "(?:[01]?[0-9]{2}|2[0-4][0-9]|25[0-5])";
        // <dotnum> ::= <snum> "." <snum> "." <snum> "." <snum>
        String dotnum_ = snum_ + "(?:\\." + snum_ + "){3}"; // + Dotted quad
        // <element> ::= <name> | "#" <number> | "[" <dotnum> "]"
        String element_ = "(?:" + name_ + "|#" + number_ + "|\\[" + dotnum_ + "\\])";
        // <domain> ::=  <element> | <element> "." <domain>
        String domain_ = element_ + "(?:\\." + element_ + ")*";
        // <mailbox> ::= <local-part> "@" <domain>
        String mailbox_ = local_part_ + "@" + domain_;
        emailMatcher = Pattern.compile(mailbox_);
        System.out.println("Email address regex = " + emailMatcher);
    }Wow. Sheesh, sabre150 that's pretty impressive. I like it for two reasons. First it avoids some false negatives that I would have gotten using the regex I mentioned. Like, [email protected] is a valid email address which my regex pattern has rejected and yours accepts. It's unusual but it's valid. And second I like the way you have compartmentalized each rule so that changes, if any custom changes are desired, are easier to make. Like if I want to specifically aim for a particular domain for whatever reason. And you've commented it so that it is easier to read, for someone like myself who knows almost nothing about regex.
Thanks, Good stuff!

Similar Messages

  • Problems with pattern matching Regular expression

    String emailRegEx = "(\\w+)@(\\w+\\.)(\\w+)(\\.\\w+)*";
    String test = "[email protected]";
    if(test.matches(emailRegEx)) {
    }throws an exception
    java.util.regex.PatternSyntaxException: Illegal repetition near index 5.

    It works fine for me.

  • Match Regular Expression does not match what Match Pattern does

    I have read through a lot of posts about how Match Pattern does not match what Match Regular Expression will due to not processing some characters.
    However, I found a problem with the other way. A simple Reg-Ex that works in Match Pattern but not Match Regular Expression.
    What I have here is just an example. I want to use Match Regular Expression so I can specify some sub-matches.
    The reg-ex is for: one or more non-numeric characters, a space, one or more numeric characters. At the start of the string.
    How can I get this working in Match Regular Expression? I am working in LabVIEW 2010f2 32 bit. Here is the code snippet and the results:
    Rob
    Solved!
    Go to Solution.

    Robert Cole wrote:
    I think I prefer the ~ for negation since ^ is also used for beginning of the string. But we work with what we have.
    Let me offer you a tip and perhaps defend the honor of the regex a little bit.  One of my favorite features of regexes is the ability to specify character classes (and their negation).  One of the reasons I have to think about the ~ versus ^ is that I rarely use ^ in a regex alternative. 
    Some examples:
    [0-9] = \d (digit)
    [^0-9] = \D (not a digit)
    The equivalent regex for your case is: \D+ \d+

  • Match pattern: change regular expression search via front panel?

    Hello,
    I have an application that I am developing which is making use of Serial VISA.
    I am scanning the output of a serial port which is constantly spitting out a long string of data. 
    Data is being pulled from the string with several combinations of SCAN FROM STRING functions and MATCH PATTERN Functions.
    Question:
    How can I use a button or TEXT box on the FRONT PANEL that can change the MATCH PATTERN Functions Regular expression?  
    for example the string may spit out the following:
     Weight\s\s\s\s\s\s\s\s\s\s\s\s-0.00\slb\s\s\s\s\s\s-16\sbits\s+74.40\s\B0F\sCorrected\s\s\s\s\s\s-0.00\slb\s+0.999987\s%\s\r
    in this case the serial device is spitting out data in LB.
    It could be possible for the device to spit out data in KG ... thus I need to change the REGULAR EXPRESSION.
    Thank you for your time

    Hi,
    just a quick example.
    there are other way of doing this but I think the ComboBox is quite an easy one.
    hope this helps
    When my feet touch the ground each morning the devil thinks "bloody hell... He's up again!"
    Attachments:
    ComboBox.vi ‏12 KB

  • "Match Regular Expression" and "Match Pattern" vi's behave differently

    Hi,
    I have a simple string matching need and by experimenting found that the "Match Regular Expression" and "Match Pattern" vi's behave somewhat differently. I'd assume that the regular expression inputs on both would behave the same. A difference I've discovered is that the "|" character (the "vertical bar" character, commonly used as an "or" operator) is recognized as such in the Match Regular Expression vi, but not in the Match Pattern vi (where it is taken literally). Furthermore, I cannot find any documentation in Help (on-line or in LabVIEW) about the "|" character usage in regular expressions. Is this documented anywhere?
    For example, suppose I want to match any of the following 4 words: "The" or "quick" or "brown" or "fox". The regular expression "The|quick|brown|fox" (without the quotes) works for the Match Regular Expression vi but not the Match Pattern vi. Below is a picture of the block diagram and the front panel results:
    The Help says that the Match Regular Expression vi performs somewhat slower than the Match Pattern vi, so I started with the latter. But since it doesn't work for me, I'll use the former. But does anyone have any idea of the speed difference? I'd assume it is negligible in such a simple example.
    Thanks!
    Solved!
    Go to Solution.

    Yep-
    You hit a point that's frustrated me a time or two as well (and incidentally, caused some hair-pulling that I can ill afford)
    The hint is in the help file:
    for Match regular expression "The Match Regular Expression function gives you more options for matching
    strings but performs more slowly than the Match Pattern function....Use regular
    expressions in this function to refine searches....
    Characters to Find
    Regular Expression
    VOLTS
    VOLTS
    A plus sign or a minus sign
    [+-]
    A sequence of one or more digits
    [0-9]+
    Zero or more spaces
    \s* or * (that is, a space followed by an asterisk)
    One or more spaces, tabs, new lines, or carriage returns
    [\t \r \n \s]+
    One or more characters other than digits
    [^0-9]+
    The word Level only if it
    appears at the beginning of the string
    ^Level
    The word Volts only if it
    appears at the end of the string
    Volts$
    The longest string within parentheses
    The first string within parentheses but not containing any
    parentheses within it
    \([^()]*\)
    A left bracket
    A right bracket
    cat, cag, cot, cog, dat, dag, dot, and dag
    [cd][ao][tg]
    cat or dog
    cat|dog
    dog, cat
    dog, cat cat dog,cat
    cat cat dog, and so on
    ((cat )*dog)
    One or more of the letter a
    followed by a space and the same number of the letter a, that is, a a, aa aa, aaa aaa, and so
    on
    (a+) \1
    For Match Pattern "This function is similar to the Search and Replace
    Pattern VI. The Match Pattern function gives you fewer options for matching
    strings but performs more quickly than the Match Regular Expression
    function. For example, the Match Pattern function does not support the
    parenthesis or vertical bar (|) characters.
    Characters to Find
    Regular Expression
    VOLTS
    VOLTS
    All uppercase and lowercase versions of volts, that is, VOLTS, Volts, volts, and so on
    [Vv][Oo][Ll][Tt][Ss]
    A space, a plus sign, or a minus sign
    [+-]
    A sequence of one or more digits
    [0-9]+
    Zero or more spaces
    \s* or * (that is, a space followed by an asterisk)
    One or more spaces, tabs, new lines, or carriage returns
    [\t \r \n \s]+
    One or more characters other than digits
    [~0-9]+
    The word Level only if it begins
    at the offset position in the string
    ^Level
    The word Volts only if it
    appears at the end of the string
    Volts$
    The longest string within parentheses
    The longest string within parentheses but not containing any
    parentheses within it
    ([~()]*)
    A left bracket
    A right bracket
    cat, dog, cot, dot, cog, and so on.
    [cd][ao][tg]
    Frustrating- but still managable.
    Jeff

  • Parse Mac Address with match regular expression

    Hi Everyone,
    I have a problem with the Match Regular Expression function,
    I am trying to parse the response two a arp -a 192.168.0.15 request in order to extract MAC address of this remote IP, I used the following RegEx: ^([0-9a-fA-F]{2}[:-]){5}([0-9a-fA-F]{2})$
    I am wondering why do I need to do a string subset first to extract only the MAC Address part. Isn't Match Regular Expression function capable of recognizing the RegEx directly in the middle of a string?
    I only works when I extract the right tring subset as in the picture bellow.
    Thanks for your answers.
    Solved!
    Go to Solution.
    Attachments:
    Mac Address.JPG ‏40 KB

    Get rid of the "^" in the beginning of your regular expression. You are instructing it to find the pattern at the beginning of the string.
    Mark Yedinak
    "Does anyone know where the love of God goes when the waves turn the minutes to hours?"
    Wreck of the Edmund Fitzgerald - Gordon Lightfoot

  • Matching Regular Expressions in OBPM 10GR3

    Hi all,
    How do I match Regular expressions like" Wait For Response" in Strings using OBPM?
    Any idea?
    Is there any exact syntax?
    Because I tried doing this:-
    for( int i=0; i < testGroup.length(); i++)
         if(testGroup.reason.match(regexp : "Wait For Response"))
              logMessage("--Test Group Matched--");
    But it does not work..
    Any idea?

    String myVar = "has matching_char_set";
    if ( myVar.match(regexp : '/matching_char_set/') ) {
    logMessage("matched...yep");
    Try like this ..should work.
    Rgds,
    Biltu

  • Match Regular Expression not returning submatches.

    I am having an issue with Match Regular Expression not returning the appropriate number of submatches.  My string is as follows:
    DATAM    995000    1.75    0.007    -67.47    24.493    99.072
    The spaces are tabs and the length is not fixed so simple string manipulation is out of the question.  The regular expression I was trying is as follows:
    (?<=\t)[0-9\.\-]*(?=(\t|$))
    It successfully returns Whole Match as the first number but no submatches are returned.  I've tried simpler expressions which work in Matlab and EditPad Pro such as [0-9.-]* but never got the same answer in Labview.
    What is going on?

    Here is an image of the VI.  The top portion is the input from our Licor 7000.  The bottom portion is the bit of code I'm having problems with.
    Attachments:
    matchregularexpression.jpg ‏336 KB

  • Question about match regular expression

    Colleagues,
    Very stupid question. I would like to get substring between "..." symbols. For example, string 02 July from Explosion occurred on "02 July", 2008.
    How to do this with single Match Regular Expression?
    For example such expression ".*" will give me "02 July":
    But I would like to get it without " symbols!
    I tried this "[~"]*" and this "[~"].*", then read this and this , and all without success... But I'm sure it should be possible. Can you help me?
    Andrey.
    PS
    This regular expression should give exactly the same output as following construction:

    I'm only using 7.0 now, but you can do this with Scan from String...
    %[^"]"%[^"]"%[^"]
    Message Edited by Phillip Brooks on 07-02-2008 02:47 PM
    Now is the right time to use %^<%Y-%m-%dT%H:%M:%S%3uZ>T
    If you don't hate time zones, you're not a real programmer.
    "You are what you don't automate"
    Inplaceness is synonymous with insidiousness
    Attachments:
    NotPCRE.png ‏20 KB

  • Multi-line String - Match Regular Expression

    I am trying to figure out the format of a regular expression in order to pull select lines out of a multi-line string and populate those lines as individual elements of a string array while using Match Regular Expression. The overall length of the multi-line string can vary as well as the text contained within the string. The string can contain letters, numbers, and special characters. I have attached an example VI. Within the example VI I only want to return the lines beginning with "Device #" into the array. The number of lines beginning with "Device #" can vary but I want to capture them all.
    Or is there a better function to use instead of Match Regular Expression that will give me the desired outcome?  
    Solved!
    Go to Solution.
    Attachments:
    MultiLine Regular Expression.vi ‏22 KB

    aaronb wrote:
    I am trying to figure out the format of a regular expression in order to pull select lines out of a multi-line string and populate those lines as individual elements of a string array while using Match Regular Expression. The overall length of the multi-line string can vary as well as the text contained within the string. The string can contain letters, numbers, and special characters. I have attached an example VI. Within the example VI I only want to return the lines beginning with "Device #" into the array. The number of lines beginning with "Device #" can vary but I want to capture them all.
    Or is there a better function to use instead of Match Regular Expression that will give me the desired outcome?  
    Match Regular Expression works well for this.
    Ben64

  • Match Regular Expression Function input string format

    Hi,
    I am new to labview and was having some difficulties using the Match Regular Experssion Function.  
    I am using labview to communicate with a sensor.  I have installed the NI device driver to do so.  The output of my sensor is in the format, 
    X20
    R40 P20 A123.  The numbers in this case are arbitrary.  I am trying to use Match Regular Expression Function to display and perform mathematical operations on the numbers.  I am having difficulties formatting the input string on the Match Regular Expression Function.  Could you please give me some tips on how to format the example I provided.  
    Thank

    MoAgha wrote:
    Hi,
    I am new to labview and was having some difficulties using the Match Regular Experssion Function.  
    I am using labview to communicate with a sensor.  I have installed the NI device driver to do so.  The output of my sensor is in the format, 
    X20
    R40 P20 A123.  The numbers in this case are arbitrary.  I am trying to use Match Regular Expression Function to display and perform mathematical operations on the numbers.  I am having difficulties formatting the input string on the Match Regular Expression Function.  Could you please give me some tips on how to format the example I provided.  
    Thank
    Here is a way to do it if the format is constant (X R P A followed by a positive integer number).
    Ben64

  • Match Regular Expression Function

    Hi guys, using this pattern I got this error:
    -4600 Error occurred during regular expression match.
    I have attached the VI.
    can you help me?thank you
    Solved!
    Go to Solution.
    Attachments:
    Untitled 2.vi ‏30 KB

    inuyasha84 wrote:
    hi well i want to save (create) a file and do a check to see if the new file that I want to create already exist or not. so the idea was to see if the path of the new file is equal to the old path
    Why not just use "Check if file or folder exists"? (File I/O -> Adv File Funcs)
    Cameron
    To err is human, but to really foul it up requires a computer.
    The optimist believes we are in the best of all possible worlds - the pessimist fears this is true.
    Profanity is the one language all programmers know best.
    An expert is someone who has made all the possible mistakes.
    To learn something about LabVIEW at no extra cost, work the online LabVIEW tutorial(s):
    LabVIEW Unit 1 - Getting Started
    Learn to Use LabVIEW with MyDAQ

  • IR filter using "matches regular expression"

    Hi,
    I am familiar with Perl regular expressions, but I'm having trouble using the IR filter by regular expression in Apex.
    For instance, I would like to search for dates of format 'MM/DD/YY' - can someone tell me how this would be done? I tried '[0-9](2)/[0-9](2)/[0-9](2)' and many other patterns to no avail.
    Also can you point me to a good thread for regular expressions in Apex?
    Thanks for any help.

    Hi,
    you can play around with oracle regular expressions at
    http://www.yocoya.com/apex/f?p=YOCOYA:REGEXP_HOME:0:
    It's an Apex application, albeit "seasoned", where you can build and test the regex and it will be 100% compatible as it runs natively, so it's not simulated on a different platform.
    Most likely the IR filter will make use of REGEXP_LIKE so you can pick that function from the menu.
    Flavio
    http://oraclequirks.blogspot.com
    http://www.yocoya.com

  • Pattern and Regular Expression problem

    Hi all!
    I have trouble with constructing a pattern.
    Consider the following:
    import java.util.regex.*;
    public class Parser {
    public Parser() {
    String pattern = "<i18n:message(.*)/>";
    String myline = "<INPUT type=\"button\" name=\"cancel\" value='<i18n:message key=\"EXP.EP.EPDATES.CANCEL\"/>' class=\"normal08\" onClick=\"javascript:disableInput();document.panel_menu.submit();\"/>";
    Pattern p = Pattern.compile(pattern);
    Matcher m = p.matcher(myline);
    while ( (m.find()) ) {
    System.out.println(m.group());
    public static void main(String[] args) {
    new Parser();
    How should I construct the pattern in order to extract from the string myline just the following sequence: <i18n:message key=\"EXP.EP.EPDATES.CANCEL\"/>
    It seems that using the pattern I wrote, it continues the match until the last appearance of "/>", and I need to have the match done until the first appearance of "/>".
    Thanks a lot.

    No, modify it to (.*?).Hey, unky. Would you mind schooling me a bit here?
    "<i18n:message(.*?)/>" right?
    against "<i18n:message key=\"EXP.EP.EPDATES.CANCEL\"/>' class=\"normal08\" onClick=\"javascript:disableInput();document.panel_menu.submit();\"/>"
    I think I understand, but would appreciate confirmation or correction.
    .* is zero or more of anything. The ? makes it reluctant (also called lazy?).
    So .* is compared against nothing, succeeds, and the regex moves on to the next character.
    This doesn't match / so regex backtracks and tries to match .* against the space, which succeeds, but then / doesn't match against the k in key
    So we backtrack, and reluctantly add the k to what .* matches.
    We keep adding a character to .*'s match every time the next character isn't a /.
    Eventually, .* matches everything up to and including the quote before the first />.
    At this point, the / and > literals match, and we're done.
    Yes?
    And we needed to make it reluctant, else the .* would have started by matching as much of the string as it could, gobbling up all of it, and then backtracking to give the /> literals their match at the end of the string.
    Yes?
    Thanks!

  • Match regular expression or pattern

    I am having big trouble with extracting measurement results from a string from serial port.
    I get a report through serial connectio from some device. It looks like that:
    Plane 1 Angle 1 Plane 2 Angle 2 In
    RotorID rpm [g·mm] [°] [g·mm] [°] Tol
    1964.7 0.145999 201.6 0.151779 346.5 Y
    2014.5 0.156608 197.6 0.139722 340.9 Y
    ealon ok 2012.1 0.143454 199.8 0.150243 344.6 Y
    ealon nok 2023.3 0.430547 217.7 0.833347 313.3 Y
    DN-3259824_001 2135.9 0.305822 246.2 0.296502 305.2 Y
    DN-3259824_002 2051.4 0.324227 241.1 0.327651 299.0 Y
    DN-3259824_020 2045.9 0.139268 150.2 0.281890 21.6 Y
     I cannot separate data with spaces, because sometimes there is no "RotorID" so all data shifts to the left.
    There are always 7 columns. Sometimes RotorID is empty sometimes it consists some chars and some numbers. I never know its length. Other data is always present and separated with space. 
    Can you help me please.
    Solved!
    Go to Solution.

    First use "Scan String For Tokens" then pull the float out of that.
    This is from my library that I use to do exactly that. 
    Omar
    Attachments:
    ALL Delimited Fract String per Line To [[DBL]].vi ‏17 KB

Maybe you are looking for

  • Problem in AP invoice validation

    Hi, I have added a new operating unit and have setup INV, PO and AP. I have entered some invoices which i want to valiadte. But the validate check box in the action window is disabled. Can anyone tell me what needs to be done. Regards Deepak

  • Has anyone had any success in correcting Maps mistakes?

    On multiple occasions I've submitted a correction using the function within Maps to report errors, but nothing is ever done.  Has anyone ever had any success in getting Apple's attention and getting something fixed?  Or is the submitting a correction

  • Text display in report

    Dear All, I have a requirement in BEx report where I have to show January 2011 instead of 01.2011. How can I make the changes in query designed to show in report. Regards Suresh

  • Cannot Send Mail, the server rejected one of the recipient's addresses.

    I receive mail just fine. Can't send anything

  • Task Instance Id.

    I need to pass the Task Instance Id (SUN IDM) in the Notification mail. Can anyone tell me how to do that. Thanks in Adv, --ravi Message was edited by: RavindraPaliwal