Match leading spaces in a regular expression?

I have the following regular expression:
^[^0-9A-Z][^0-9A-Z ]$
For some reason, it does not match leading spaces, so " A" is considered no match, even though space is obviously neither a digit or an uppercase letter...
[Edit] Also, it doesn't seem to match case properly - "ab" is matched, but not "Ab" or "aB"...

Then remove the not's ^
From : ^[^0-9A-Z][^0-9A-Z ]$
To     : ^[0-9A-Z][0-9A-Z ]$
The first ^ indicates the beginning of the line and $ the end of the line.  The pattern will only work with two characters on the line.  If you have more than two characters on the line then use this
To     : ^[0-9A-Z][0-9A-Z ]
jdweng

Similar Messages

  • Match beginning of line with Regular Expression

    I'm confused about dreamweaver's treatment of the characters
    ^ and $ (beginning of line, end of line) in regex searches. It
    seems that these characters match the beginning of the file, not
    the beginning of the various lines in the file. I would expect it
    to work the other way around. A search like:
    (^.)
    should match every line in the file, so that a find/replace
    could be performed at the beginning of each line, like this:
    HELLO$1
    which would add 'HELLO' at the start of each line in the
    file.
    Instead, this action only matches the first character of the
    file, sticks 'HELLO' in front of it, and then quits (or moves on to
    the next file). The endline character $ behaves in a similar
    fashion, matching only the end of the file, not the end of each
    line.
    I've searched, and all the literature about regular
    expressions in dreamweaver seems to indicate that I'm expecting the
    correct behavior:
    www.adobe.com/devnet/dreamweaver/articles/regular_expressions_03.html
    quote:
    ^ Beginning of input or line ^T matches "T" in "This good
    earth" but not in "Uncle Tom's Cabin"
    $ End of input or line h$ matches "h" in "teach" but not in
    "teacher"
    Thanks for any insight, folks.

    Hi Winston,
    I am still digesting the material from the regular expression book and will take sometime to become proficient with it.
    It seems that using groupCount() to eliminate the unwanted text does not work in this case, since all the lines returned the same value. Ie 3 posted earlier. This may be because the patterns are complex and only a few were grouped together. Otherwise, could you provide an example using the string posted as opposed to a hyperthetic one. In the meantime, at least one solution have been found by defining an additional special pattern “\\A[^%].*\\Z”, before combining / intersecting both existing and the new special pattern to get the best of both world. Another approach that should also work is to evaluate the size of String.split() and only accept those lines with a minimum number of tokens.
    Anyhow, I have come a crossed another minor stumbling block in the mean time with the following line, where some hidden characters is preventing the existing pattern from reading it:
    o;?Mervan Bay 40 Boyde St 7 br t $250,000 X West Park AE
    Below is the existing regular expression that works for other lines with the same pattern but not for special hidden characters such as “o;?”:
    \\A([A-Z][a-z]*){1,2} [0-9]{0,4}/?[0-9]{0,4}-?[0-9]{0,4} ([A-Z][a-z]*){1,2} St|Rd|Av|Sq|Cl|Pl|Cr|Gr|Dr|Hwy|Pde|Wy|La [0-9] br [h|u|t] \\$\\d+,\\d+|\\$\\d*\\,\\d+,\\d+ ([A-Z][a-z]*){1,}\\ZIs it possible to come up with a regular expression to ignore them so that this line could be picked up? Would also like to know whether I could combine both the special pattern “\\A[^%].*\\Z” with existing one as opposed to using 2 separate patterns altogether?
    Many thanks,
    Jack

  • Space Separated Hex Regular Expression

    Hi peeps. I have to do a bit of validation on a user input. It has to be space separated hex and has to be 16 in length. e.g.:
    1 2 1A 2E F 8 3 4 1A 12 12 12 12 12 12 12
    So far I have something like this:
    String spaceSeparatedHexString = "\\s*[0-9A-F[0-9A-F]?\\s+]+\\s*";This checks to make sure it is in space separated hex but does not check the length of it.
    If I were to change it to this:
    String spaceSeparatedHexString = "\\s*[0-9A-F[0-9A-F]?\\s+]{16}\\s*";It would not work because it just counts to 16 characters.
    How do I make it so it checks that there are 16 space separated hex values?
    Thanks, Boomah

    Thanks guys. You pointed me in the right direction and I came up with the following that functions as I want:
    String spaceSeparatedHexString = "\\s*([0-9A-F]{1,2}\\s+){15}[0-9A-F]{1,2}\\s*";Cheers,
    Boomah :o)

  • Regular expression in exact match

    Hi,
    Please let me know how do i find an exact match within a string using Regular expression,
    var str1:String = "s1 + s2 + sf_s1 + s1 - s2";
    var replceableStr:String = "s1";
    var reggular:RegExp = /\s+(s1)\s+/gi;
    var str2:String;
    str2 = str1.replace(reggular,"format");
    i want only s1 to be replaced with format but sf_s1 should not be replaced, please let me know which Reg exp to use
    Expected output: format + s2 +sf_s1 + format -s2

    Odd, though. I thought regex wouldn't pick upthe
    \n unless you explicitly told it to with DOTALL or
    MULTILINE or (?s) or (?n).
    I thought "String.matches" matched the wholestring.
    So, since the string had a newline, it didn'tmatch.
    It does.Ah, that would explain it then.

  • Esing regular expression to reformat text (match & replace)

    Hi guys ..I am using regilar expression to reformat logs files..
    this is how i match the pattern..
    Pattern pattern = Pattern.compile("^<[^>]*>(.{6}).....
    Matcher matcher = pattern.matcher(thisLine);
    then prints the output...
    myOutput.print(matcher.group(1));
    It works..however my prob now is, i am reading a date in format "8 Nov"
    How can i change this to 8-11-2005 ????

    hmmm....ya..i can see the idea
    (\\d\\d?) (Jan|Feb|Mar|Apr|May|Ju[nl]|Aug|Sep|Oct|Nov|Dec) means that the month can be either one rite?
    but the thing is..its collected and put in a group. for example:
    Pattern pattern = Pattern.compile("^<[^>]*>(.{6}) ...
    myOutput.print(matcher.group(1));
    the first regular expression is group in group(1). its working..it can detect the text in group(1) the problem was...i tried this..
    String priority = matcher.group(6);
                   if (priority=="3")
                        System.out.print("AAA\n");
                   else
                   System.out.print("BBB\n");
    it keeps on printing BBB even there is a "priority=3".

  • Regular expression to match incremental or consecutive digits

    I need to process a string containing all digits to ensure that it does not contain either
    (a) a group of 5 or more consecutive identical digits eg. 11111
    (b) a group of 5 or more incremental/decremental digits eg 12345, 98765
    Also, is there a processing difference between the reluctant and greedy qualifiers?
    Case (a) seems to be easy:
    Pattern pattern = Pattern.compile("[0-9]{5,}?");but what about (b) ? From the documentation it seems that using capturing groups might be helpful, but they are confusing me.
    Finally how do I merge multiple pattern matching strings into one overall regular expression so I can make one pass on the input to see whether it is valid or not?
    Thanks
    Chris

    give this code a try
    public class Test {
         static boolean check(String str) {
              loop : for (int x = 0, y; x < str.length() - 4; x += y) {
                   y = 1;
                     int dif = (str.charAt(x+y) - str.charAt(x)); //assuming you don't whant 90123
                     if (dif >= -1 && dif <= 1) {
                        for (; y < 4; y++) {
                             if ((str.charAt(x+y+1) - str.charAt(x+y)) != dif) {
                                  continue loop;
                        return true;
              return false;
         public static void main(String[] args) {
              System.out.println(check(args[0]));
    }

  • REGULAR EXPRESSION FIND PLEASE  ;(

    HI FORM
    I have the following documents with the content
    doc1 = "ELECTRONICS DIGITAL CAMERA"
    doc2 = "ELECTRONICS DIGITAL CAMERA ACCESSIORIES"
    doc3 = "ELECTRONICS DIGITAL CAMERA OPTICS "
    Using regexpression I would like to get only 2nd document ONLY which has the content
    "ELECTRONICS DIGITAL CAMERA ACCESSIORIES"
    How to Achieve this
    Karthik

    You can try this one: ((digital|camera|accessories)[\\s]*)+
    Explainations:
    (digital|camera|accessories) - this group matches any of the 3 words
    [\\s] matches a space character [\\s]* matches any number of spaces
    ((digital|camera|accessories)[\\s]*) - this group matches any of the 3 words, optionnally followed by spaces
    ((digital|camera|accessories)[\\s]*)+ - matches any sequence of 3 words, seperated by spaces
    NOTES:
    - this regular expression matches "digitalcameraaccessories" because the * operator accepts 0 occurences.
    If you want to avoid this situation, change the * to a+, but you will have to append a space to the searched string
    in order to make the pattern match.
    - this regular expression will also match "digital digital camera" because there is no unicity checking.
    Hope this helped,
    Regards.

  • Troubles with regular expressions

    I seem to be having problems writing my regular expression. I check it on a regex testing website and it should work. Maybe I am doing something wrong because it is in Java and not Perl? I was wondering if someone could give me a little help?
    Pattern p = Pattern.compile("<.*posttitle.*h2>", Pattern.DOTALL);
            Matcher m = p.matcher(content);
            boolean b = m.matches();That's what my regular expression looks like but the boolean b keeps returning false. The text I am looking at is the source code of [dailypoetryclub.com|http://dailypoetryclub.com] and I want to basically pull out just the links of each day's topic. Any ideas what I'm doing wrong?

    1) Sounds like you need find() not matches() (matches look for the whole string to match exactly) if you are looking for the pattern in a long string.
    2) You should make the ".*" reluctant or it will take more than you expect.

  • Regular Expression for replacing Leading Spaces

    I don't claim to be any expert on Regular Expressions and even after reading CD's introduction to Reg Exp, I still can't figure out this one which I'm sure must be very basic.
    I want to replace all the leading spaces in a string with "." chrs. I could do this using the common replace/substr/instr functions, but I reckoned it would be possible in a single regular regexp_replace call.
    So far I've got this...
    SQL> select regexp_replace('      FRED BLOGS    WAS HERE    ', '^([:space:])*', '.')
      2  as result
      3  from dual;
    RESULT
    .      FRED BLOGS    WAS HERE
    SQL>Which is replacing the start of line with a "." and not the spaces.
    But I want my result to be:-
    RESULT
    ......FRED BLOGS    WAS HERE
    SQL>Cheers

    That was very good solution .
    Can you explain me the significance of "| " in the code, other things I could trace out.
    I try to run the code with the 2 cases
    when I give a space after | symbol it prints the * many times
    SQL> SELECT col1, REGEXP_REPLACE(col1, ' ([^ ]+.*)|','*\1')
    2 FROM (SELECT ' FRED BLOGS WAS HERE ' col1 FROM dual );
    COL1
    REGEXP_REPLACE(COL1,'([^]+.*)|','*\1')
    FRED BLOGS WAS HERE
    *FRED BLOGS    WAS HERE
    SQL> SELECT col1, REGEXP_REPLACE(col1, ' ([^ ]+.*)| ','*\1')
    2 FROM (SELECT ' FRED BLOGS WAS HERE ' col1 FROM dual );
    COL1
    REGEXP_REPLACE(COL1,'([^]+.*)|','*\1')
    FRED BLOGS WAS HERE
    ******FRED BLOGS WAS HERE

  • Regular Expression To Remove Leading While Space

    Hii
    I have a data like this
    20200 Kuala Terengganu
    Terengganu
    Luala Terengganu
    I want to remove the Leading white space in the Second line .Can anybody help me to
    write regular expression for this?
    I want my result like the following :
    20200 Kuala Terengganu
    Terengganu
    Luala Terengganu
    Thanks In Advance
    Jim John

    But there's no difference between two group of lines which you posted. Could you please format your input and desired output with please?
    Kamran Agayev A. (10g OCP)
    http://kamranagayev.wordpress.com                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           

  • Rplacing space with &nbsb; in html using regular expressions

    Hi
    I want to replace space with &nbsb; in HTML.
    I used  the below method to replace space in my html file.
    var spacePattern11:RegExp =/(\s)/g; 
    str= str.replace(spacePattern," "
    Here str varaible contains below html file.In this html file i want to replace space present between " What number does this  represents" with &nbsb;
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Untitled Document</title>
    </head>
    <body>
    <b><TEXTFORMAT LEADING="2"><P ALIGN="LEFT"><FONT FACE="Verdana" style = 'font-size:10px' COLOR="#0B333C" LETTERSPACING="0" KERNING="0"><B></B></FONT></P></TEXTFORMAT><TEXTFORMAT LEADING="2"><P ALIGN="LEFT"><FONT FACE="Verdana" style = 'font-size:10px' COLOR="#0B333C" LETTERSPACING="0" KERNING="0"><B> What number does this Roman numeral represents MDCCCXVIII ?</B></FONT></P></TEXTFORMAT></b>
    </body>
    </html>
    But by using the above regular expression i am getting like this.
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Untitled Document</title>
    </head><body>
    <b><TEXTFORMAT LEADING="2"><P ALIGN="LEFT"><FONT FACE="Verdana" style = 'font-size:10px' COLOR="#0B333C" LETTERSPACING="0" KERNING="0"><B></B></FONT></P></TEXTFORMAT><TEXTFORMAT LEADING="2"><P A LIGN="LEFT"><FONT FACE="Verdana" style = 'font-size:10px' COLOR="#0B333C" LETTERSPACING="0 " KERNING="0"><B> What number does this represents</B></FONT></P></TEXTFORMAT></b>
    </body>
    </html>
    Here what happening means it was replacing space with &nbsb; in HTML tags also.But want to replace space with &nbsb; present in the outside of the HTML tags.I want like this using regular expressions in FLEX
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Untitled Document</title>
    </head>
    <body>What number does this represents</body>
    </html>
    Hi,Please give me the solution to slove the above problem using regular expressions
    Thanks in Advance to all
    Regards
    ssssssss

    sorry i missed some information in above,The modified information was in red color
    Hi
    I want to replace space with &nbsb; in HTML.
    I used  the below method to replace space in my html file.
    var spacePattern11:RegExp =/(\s)/g; 
    str= str.replace(spacePattern," "
    Here str varaible contains below html file.In this html file i want to replace space present between " What number does this  represents" with &nbsb;
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Untitled Document</title>
    </head>
    <body>
    <b><TEXTFORMAT LEADING="2"><P ALIGN="LEFT"><FONT FACE="Verdana" style = 'font-size:10px' COLOR="#0B333C" LETTERSPACING="0" KERNING="0"><B></B></FONT></P></TEXTFORMAT><TEXTFORMAT LEADING="2"><P ALIGN="LEFT"><FONT FACE="Verdana" style = 'font-size:10px' COLOR="#0B333C" LETTERSPACING="0" KERNING="0"><B> What number does this Roman numeral represents MDCCCXVIII ?</B></FONT></P></TEXTFORMAT></b>
    </body>
    </html>
    But by using the above regular expression i am getting like this.
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Untitled Document</title>
    </head><body>
    <b><TEXTFORMAT LEADING="2"><P ALIGN="LEFT"><FONT FACE="Verdana" style = 'font-size:10px' COLOR="#0B333C" LETTERSPACING="0" KERNING="0"><B></B></FONT></P></TEXTFORMAT><TEXTFORMAT LEADIN G="2"><P ALIGN="LEFT"><FONT FACE="Verdana" style = 'font-size:10px' COLOR="#0B33 3C" LETTERSPACING="0" KERNING="0"><B> What number does this represents</B></FONT></P></TEXTFORMAT></b>
    </body>
    </html>
    Here what happening means it was replacing space with &nbsb; in HTML tags also.But want to replace space with &nbsb; present in the outside of the HTML tags.I want like this using regular expressions in FLEX
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Untitled Document</title>
    </head>
    <body>What&nbsb;number&nbsb;does&nbsb;this&nbsb;represents</body>
    </html>
    Hi,Please give me the solution to slove the above problem using regular expressions
    Thanks in Advance to all
    Regards
    ssssssss

  • Regular expressions and string matching

    Hi everyone,
    Here is my problem, I have a partially decrypted piece string which would appear something like.
    Partially deycrpted:     the?anage??esideshe?e
    Plain text:          themanagerresideshere
    So you can see that there are a few letter missing from the decryped text. What I am trying to do it insert spaces into the string so that I get:
                        The ?anage? ?esides he?e
    I have a method which splits up the string in substrings of varying lengths and then compares the substring with a word from a dictionary (implemented as an arraylist) and then inserts a space.
    The problem is that my function does not find the words in the dictionary because my string is only partially decryped.
         Eg:     ?anage? is not stored in the dictionary, but the word �manager� is.
    So my question is, is there a way to build a regular expression which would match the partially decrypted text with a word from a dictionary (ie - ?anage? is recognised and �manager� from the dictionary).

    I wrote the following method in order to test the matching using . in my regular expression.
    public void getWords(int y)
    int x = 0;
    for(y=y; y < buff.length(); y++){
    String strToCompare = buff.substring(x,y); //where buff holds the partially decrypted text
    x++;
    Pattern p = Pattern.compile(strToCompare);
    for(int z = 0; z < dict.size(); z++){
    String str = (String) dict.get(z); //where dict hold all the words in the dictionary
    Matcher m = p.matcher(str);
    if(m.matches()){
    System.out.println(str);
    System.out.println(strToCompare);
    // System.out.println(buff);
    If I run the method where my parameter = 12, I am given the following output.
    aestheticism
    aestheti.is.
    demographics
    de.o.ra.....
    Which suggests that the method is working correctly.
    However, after running for a short time, the method cuts and gives me the error:
    PatternSyntaxException:
    Null(in java.util.regex.Pattern).
    Does anyone know why this would occur?

  • [Regular Expressions] Saving a variable number of matches

    I'm stuck with the following problem and I don't seem to be able to solve without lots of ifs and else's.
    I've got a program that you can pass patterns as parameters to. The program receives patterns as one single string.
    The string could look like this:
    a:i:foo r::bar t:ei:bark
    or like this:
    a:i:foo
    What I'm hinting at is that the string comprises of several parts of the same structure. Each structure can be matched and saved with:
    ([art]:[ei]{0,2}:.*)
    Now I want my regular expression able to match all the occurences without checking the string containing the pattern for something that could indicate the number of structures inside it. The following does not seem to work:
    ([art]:[ei]{0,2}:.*)+
    So now I'm looking for something that would match one or more occurence of the structure and save it for future use.
    I'd be really happy if someone could help me out here
    Last edited by n0stradamus (2012-05-03 20:27:02)

    Procyon wrote:
    --> echo "a:i:foo r::bar t:ei:bark" | sed 's/\([art]:[ei]\{0,2\}:[^ ]*\)/1/'
    1 r::bar t:ei:bark
    --> echo "a:i:foo r::bar t:ei:bark" | sed 's/\([art]:[ei]\{0,2\}:[^ ]*\)/1/g'
    1 1 1
    If [^ ]* is not usable (spaces are allowed arbitrarily), you need a non-greedy .* and non-consuming look-ahead of " [art]:"
    In python's re module, this is .*?(?=( [art]:|$))
    >>> import re
    >>> m=re.findall("([art]:[ei]{0,2}:.*?(?=( [art]:|$)))","a:i:foo r::bar t:ei:bark")
    >>> print(m)
    [('a:i:foo', ' r:'), ('r::bar', ' t:'), ('t:ei:bark', '')]
    Exactly what I was looking for! I didn't know that you could specify .* to stop at a certain sequence of characters.
    Could you please point me to some materials where I can read up on the topic?
    Back to the regex: It works finde in Python, but sadly that is not the language I'm using
    The program I need this for is written in C and until now the regex functions from glibc worked fine for me.
    Have I missed a function similar to re.findall in glibc?

  • 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+

  • Regular expression to replace "emtpy space" ( ) bitween words with +

    Hallo!
    When I wish to find in code something like this:
    12144541 FirstWord SecondWord
    regular expression for that is:
    (\d{1,100})[\s-]\D{1,100}[\s-]\D{1,100}
    Now, please help me tu find regular expression to replace
    "emtpy space" ( ) bitween words with +
    12144541 FirstWord SecondWord to become
    12144541+FirstWord+SecondWord
    Thank you very, very, very much!

    A simple-minded solution is to use \s to match all
    whitespace; e.g. find \s and replace with +. DW CS3, at least, is
    smart enough to not replace end of line characters with the '+'
    character if you limit your search & replace to text.

Maybe you are looking for