White space in regular expressions (Pattern class)

Hello,
I have to check if a String contains ONLY the following characters: a-z, A-Z, ' and ( ) .
The regular expression that I wanted to use with the Pattern class was [[a-zA-Z][\u0027][\u0028][\u0029][\s]] .
Now, my compiler (Eclipse) doesnt recognize \s as an expression. Is this the proper expression for a white space?
Also, in this notation, will the Pattern check for the order of the characters, cause the order isn't supposed to matter.
I would appreciate any help you could give me on this subject.
With Best Regards, Roderick

I'm not a regex expert, but I don't see any of the regex gurus online, and this I can tell you.
my compiler (Eclipse) doesnt recognize \s as an expressionYou need to escape the backspace in a String literal. Use"\\s"
in this notation, will the Pattern check for the order of the charactersFor that I think (note:think, not know) you need to put the entire set of characters to be matched in one character class. Could you try this and post back whether it works for your requirement?"[^a-zA-Z'()\\s]"Note the negation operator at the start of the characer class, which will match positive for any character not in the character class.
db
edit You can test your regex here:
{color:0000ff}http://www.dotnetcoders.com/web/Learning/Regex/RegexTester.aspx{color}
but remember to double the backslashes whe you include it in your java code as a String literal.
Edited by: Darryl.Burke

Similar Messages

  • Unable to parse control character using regular expression & Pattern class

    I am trying to parse the Ctrl-X character from a string using the Pattern class but for some or the other reason it is not finding it.
    Here is the code that I wrote,
    Pattern p=Pattern.compile("\\cX");
    Matcher m=p.matcher(str);
    System.out.println(str+": "+m.find());
    the result is prop^Xau,bu,ca,en,fe,fi,ge,hj,ma,pe,re,sh,sr,tr^Yto^Xym^Yfmt^Xh : false
    where, str= prop^Xau,bu,ca,en,fe,fi,ge,hj,ma,pe,re,sh,sr,tr^Yto^Xym^Yfmt^Xh
    Thanks,
    Arunraj

    arunraj.nair wrote:
    I am trying to parse the Ctrl-X character from a string using the Pattern class but for some or the other reason it is not finding it.
    Here is the code that I wrote,
    Pattern p=Pattern.compile("\\cX");
    Matcher m=p.matcher(str);
    System.out.println(str+": "+m.find());
    the result is prop^Xau,bu,ca,en,fe,fi,ge,hj,ma,pe,re,sh,sr,tr^Yto^Xym^Yfmt^Xh : false
    where, str= prop^Xau,bu,ca,en,fe,fi,ge,hj,ma,pe,re,sh,sr,tr^Yto^Xym^Yfmt^XhWhat does str.length() give you? And posting, and trying with, a (much) shorter String might help you and others here to more easily see the problem.
    db

  • Regular Expressions Character Class shortcuts

    I have been learning to use regular expressions to modify some of my text files. I noticed that on my ARCH box the Character Class shortcuts do not work e.g. [[:digit:]] in an expression works but \d does not. Is this normal or is my installation broken in some way?

    Bebo wrote:
    There are several regexp "dialects". It's quite painful actually For instance, as far as I know, \d works in perl, but not in sed or grep.
    So, yes, this is normal.
    Yeah -- Henry Spencer's regexp stuff is always generally considered the portable form for sed, awk, since they're all based from it.  Newer versions of grep though do allow for a -P flag for perl-regexps to be used, but this is non-portable, obviously.
    -- Thomas Adam

  • -Split Regular expression pattern

    Hi all,
    I came across the below example in Lee Holmes' PowerShell Cookbook 3rd editon:
    "Hello World" -split  "He(ll.*o)r(ld)"
    and the output is:
    llo Wold
    Even though I do understand Regular Expressions to some degree (where I can comfortably write a reasonably simple pattern for -replace operator or Select-String cmdlet for example), I am struggling to understand the RE pattern above and what it's trying
    to achieve in the -split context. What are the 2 groupings trying to achieve here while splitting a text? What I want to know is the literal translation of the pattern above in clear English words.
    Any thoughts?

    I guess that explains it clearly now. In fact that's what I wrote above yesterday. So parenthesis in -split operator pattern is not a grouping construct, instead a 'preserving' construct, saving it from consumption by the split operation.
    Therefore, following -match operator pattern-semantics  to understand -split operator will mislead us here.
    But then again, I have troubles understanding the below pattern  
    "Hello World" -split  "(He(ll.*o)r(ld))"Hello World
    llo Wo
    ld
    However, I think I will leave it at this stage. The main thing I wanted to know was what those parenthesis were there for and now I have the answer. It did not really makes sense to group a pattern for -split operator hence I started this thread.
    I think that's all I wanted to know. Thank you.

  • Regular Expression Pattern

    I currently have a RegExp pattern set up with the expression (\w\s\!\$/\(\)\&\.\+\-]+) (there is a [ between the ( and \w but it was showing HTML: instead of [ (maybe the problem?)) but when I try to compile the page, I get an XML-20201: (Fatal Error) Expected name instead of \ error
    Does anybody have any idea what I need to change in the regular expression to get it to run in Jdeveloper?
    Thanks
    Andy

    <messageTextInput id="${bindings.ItemCodesCodeDescription.path}" model="${bindings.ItemCodesCodeDescription}" required="yes" readOnly="${jhsUserRoles['ITEMS_RO']}" promptAndAccessKey ="Des&amp;cription" rows="1" maximumLength="50" columns="30">
    <onSubmitValidater>
    <regExp pattern="(\w\s\!\$/\(\)\&\.\+\-]+)"/> //again, [ between ( and \w
    </onSubmitValidater>
    </messageTextInput>
    It's part of a Description textInput, we want to limit the use of characters because it will be saved to our database

  • Regular Expression Pattern maching class to Validate Alphanumeric String

    Hi
    MY DB version is Oracle Database 10g Enterprise Edition Release 10.2.0.4.0 - 64bit Production
    I wanted to use the pattern maching classes given by oracle to check whether a given input string is alphanumeric or not.
    Oracle 10g documentation says about the classes like [:digit:], [:alnum:] and many others.
    http://www.dba-oracle.com/t_regular_expressions.htm
    However these classes seems not to be working with my DB version.
    Can someone tell me the DB version in which these classes works ?
    Below is the code I am using to validate a input string.
    SQL> CREATE OR REPLACE FUNCTION fn_is_alphanumeric
      2  (
      3     pi_value             IN       VARCHAR2
      4  )
      5  RETURN VARCHAR2
      6  IS
      7     lv_length   NUMBER;
      8  BEGIN
      9     lv_length := length(pi_value);
    10     IF ( REGEXP_LIKE(pi_value,'[:alnum:]{'||lv_length||'}')) THEN
    11        RETURN 'TRUE';
    12     ELSE
    13        RETURN 'FALSE';
    14     END IF;
    15  END fn_is_alphanumeric;
    16  /
    Function created.
    SQL>
    SQL>
    SQL> SELECT fn_is_alphanumeric('abc123') alpha FROM DUAL;
    ALPHA
    FALSE
    SQL>
    SQL>
    SQL> CREATE OR REPLACE FUNCTION fn_is_alphanumeric
      2  (
      3     pi_value             IN       VARCHAR2
      4  )
      5  RETURN VARCHAR2
      6  IS
      7     lv_length   NUMBER;
      8  BEGIN
      9     lv_length := length(pi_value);
    10     IF ( REGEXP_LIKE(pi_value,'[A-z0-9]{'||lv_length||'}')) THEN
    11        RETURN 'TRUE';
    12     ELSE
    13        RETURN 'FALSE';
    14     END IF;
    15  END fn_is_alphanumeric;
    16  /
    Function created.
    SQL> SELECT fn_is_alphanumeric('abc123') alpha FROM DUAL;
    ALPHA
    TRUE

    Character classes are working fine with regexp engine in your database version - simply change your function definition to:
    CREATE OR REPLACE FUNCTION fn_is_alphanumeric
       pi_value             IN       VARCHAR2
    RETURN VARCHAR2
    IS
       lv_length   NUMBER;
    BEGIN
       lv_length := length(pi_value);
       IF ( REGEXP_LIKE(pi_value,'[[:alnum:]]{'||lv_length||'}')) THEN
          RETURN 'TRUE';
       ELSE
          RETURN 'FALSE';
       END IF;
    END fn_is_alphanumeric;
    /(brackets should be doubled as compared with your original version).
    You can use both , range-based and class-based expressions, but you have to keep in mind, that range-based are nls dependant as opposite to class-based.
    SQL> alter session set nls_sort=binary;
    Session altered.
    SQL> with t as (
      2   select 'üäö123' s from dual
      3  )
      4  select
      5  case
      6  when regexp_like(s,'^[a-zA-Z0-9]+$') then 'Alphanumeric'
      7  else 'Not Alphanumeric'
      8  end range,
      9  case
    10  when regexp_like(s,'^[[:alnum:]]+$') then 'Alphanumeric'
    11  else 'Not Alphanumeric'
    12  end class
    13  from t
    14  /
    RANGE                CLASS
    Not Alphanumeric     Alphanumeric
    SQL>
    SQL> alter session set nls_sort='GERMAN';
    Session altered.
    SQL> with t as (
      2   select 'üäö123' s from dual
      3  )
      4  select
      5  case
      6  when regexp_like(s,'^[a-zA-Z0-9]+$') then 'Alphanumeric'
      7  else 'Not Alphanumeric'
      8  end range,
      9  case
    10  when regexp_like(s,'^[[:alnum:]]+$') then 'Alphanumeric'
    11  else 'Not Alphanumeric'
    12  end class
    13  from t
    14  /
    RANGE                CLASS
    Alphanumeric         AlphanumericBest regards
    Maxim

  • Regular Expressions (Pattern/Matcher) --- Help

    Hi,
    I have an regex i.e. Pattern.compile("([0-9])D([0-9])'?(?:([0-9]+)\")?([NSEW])").{code}
    It has to exactly match the input e.g *45D15'34"N*
    I need to retrieve the values based on grouping.
    Group1 = 45 (degree value)
    Group2 = 15 (minutes value)
    Group3 = 34 (seconds value) ----> this is a non-capturing group
    Group4 = N (directions)
    The regex works fine for most of longitude/latitude value but I get a StackOverFlow for some. There is a known bug on this http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=5050507
    According to the bug report, they have said that are many different regex that can trigger the stack overflow....even though the length of my input is not as long as the one posted on the bug report.
    I was wondering if anyone could suggest a different way of writing the regex above to avoid the stack over flow.
    Thank you in advance                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               

    Hi,
    I missed the '+' in my original regex Pattern.compile("([0-9]+)D([0-9]+)'?(?:([0-9]+)\")?([NSEW])"){code}.
    I have also tried {code} Pattern.compile("(\\d+)D(\\d+)'?(?:(\\d+)\")?([NSEW])");And, the other 2 expressions as suggested by you.
    The problem happens when Durham Lat=”35D52’N” Lon=”78D47’W value is selected from a Jtree(the values are parsed from a xml file to the tree - the xml file has a bout 800 longitude/latitude elements for different cities in the US). It does not happen for other values and If I increment the degree or min by, then the expression works. I am not sure how else i could re-write this exp.
    Below is the snippet of the xml file:
    <State name="NORTH CAROLINA">
                <City name="Asheville AP"     Lat="35D26'N"     Lon="82D32'W"/>
                <City name="Charlotte AP"     Lat="35D13'N"     Lon="80D56'W"/>
                <City name="Durham"     Lat="35D52'N"     Lon="78D47'W"/>
                <City name="Elizabeth City AP"     Lat="36D16'N"     Lon="76D11'W"/>
                <City name="Fayetteville, Pope AFB" Lat="35D10'N"     Lon="79D01'W"/>
                <City name="Goldsboro,Seymour-Johnson"     Lat="35D20'N"     Lon="77D58'W"/>
                <City name="Greensboro AP (S)"     Lat="36D05'N"     Lon="79D57'W"/>
                <City name="Greenville"     Lat="35D37'N"     Lon="77D25'W"/>
                <City name="Henderson"     Lat="36D22'N"     Lon="78D25'W"/>
                <City name="Hickory"     Lat="35D45'N"     Lon="81D23'W"/>
                <City name="Jacksonville"     Lat="34D50'N"     Lon="77D37'W"/>
                <City name="Lumberton"     Lat="34D37'N"     Lon="79D04'W"/>
                <City name="New Bern AP"     Lat="35D05'N"     Lon="77D03'W"/>
                <City name="Raleigh/Durham AP (S)"     Lat="35D52'N"     Lon="78D47'W"/>
                <City name="Rocky Mount"     Lat="35D58'N"     Lon="77D48'W"/>
                <City name="Wilmington AP"     Lat="34D16'N"     Lon="77D55'W"/>
                <City name="Winston-Salem AP"     Lat="36D08'N"     Lon="80D13'W"/>
            </State>
    public final class GeoLine {
        /* Enum for the possible directions of longitude and latitude*/
        public enum Direction {
            N, S, E, W;
            public boolean isLongitude() {
                return (this == E || this == W);
            public boolean isLatitude() {
                return (this == N || this == S);
            public Direction getCanonicalDirection() {
                if (this == S) {
                    return Direction.N;
                } else if (this == W) {
                    return Direction.E;
                } else {
                    return this;
        private final int degree;
        private final int minute;
        private final int second;
        private final Direction dir;
        /* Recognizes longitude and latitude values that has degrees, minutes and seconds i.e. "45D15'34"N
        * or "45D1534"N. The single-quotes for the minutes is optional. And, for the moment we do not support seconds
        * validation although ilog library returns the longitude/latitude with second when NEs and Sub-networks are
        * dragged and dropped on the map.*/
    private static final Pattern PATTERN = Pattern.compile("([0-9]+)D([0-9]+)'?(?:([0-9]+)\")?([NSEW])");
        public GeoLine(int degree, int minute, Direction dir) {
            this(degree, minute, 0, dir);
        public GeoLine(int degree, int minute, int second, Direction dir) {
            Log.logInfo(getClass().getSimpleName(), "PAU degree: " + degree + " minute: " + minute + " second: " + second + " direction: " +  dir);
            verifyLongitudeLatitude(degree, dir);
            verifyMinute(degree, minute, dir);   
            this.degree = degree;
            this.minute = minute;
            this.second = second;
            if (this.degree == 0 && this.minute == 0 && this.second == 0) {
                this.dir = dir.getCanonicalDirection();
            } else {
                this.dir = dir;
        public Direction getDirection() {
            return dir;
        public int getMinute() {
            return minute;
        public int getDegree() {
            return degree;
        public int getSecond() {
            return second;
        public static GeoLine parseLine(String location) {
            * Matcher class will throw java.lang.NullPointerException if a null location
            *  is passed, null location validation is not needed.
            Matcher m = PATTERN.matcher(location);
            if(m.matches()) {
                int deg;
                int min;
                int second;
                Direction direction;
                deg = Integer.parseInt(m.group(1));
                min = Integer.parseInt(m.group(2));
                if (m.group(3) == null) {
                    second = 0;
                } else {
                    second = Integer.parseInt(m.group(3));
                direction = Direction.valueOf(m.group(4));
                return new GeoLine(deg, min, second, direction);
            } else {
                throw new IllegalArgumentException("Invalid location value. Expected format XXDXX'XX\"[NSEW] " + location);
        private void verifyMinute(int deg, int min, Direction direction) {
            /* This validation is to make sure that minute does not exceed 0 if maximum value for latitude == 90
            * or longitude == 180 is specified */
            int maxDeg = direction.isLatitude() ? 90 : 180;
            if(min < 0 || min > 59) {
               throw new NumberFormatException("Minutes is out of range. Value should be less than 60: " + min);
            if (deg == maxDeg && min > 0) {
                throw new NumberFormatException("Degree value " + deg + "D" + direction + " cannot have minute exceeding 0: " + min);
        private void verifyLongitudeLatitude(int valDeg, Direction valDir) {
               int max = valDir.isLatitude() ? 90 : 180;
               if(valDeg < 0 || valDeg > max) {
                    throw new NumberFormatException("Degree " + valDeg + valDir + " is invalid");
        public final boolean isLongitude() {
            return dir.isLongitude();
        public final boolean isLatitude() {
            return dir.isLatitude();
        @Override
        public final String toString(){
            if(minute < 10){
                return degree + "D0" + minute + dir;
            } else {
                return degree + "D" + minute + dir;
        @Override
        public boolean equals(Object obj) {
            if (obj instanceof GeoLine) {
                GeoLine other = (GeoLine) obj;          
                    return (this.degree == other.degree && this.minute == other.minute && this.second == other.second && this.dir == other.dir);
            return false;
        @Override
        public int hashCode() {
            int result = 17;
            result = result * 37 + degree;
            result = result * 37 + minute;
            result = result * 37 + second;
            result = result * 37 + dir.hashCode();
            return result;
    }Thank you again.

  • Regular Expression Pattern Question

    I'm building up a file URL String in my program. I'd like to remove all occurrences of "\\" in a String with "/" using the java.lang.String.replaceAll method:
    String fileName = "junk.txt";
    static String DEFAULT_FILE_URL_ROOT = "file:/" + System.getProperty("user.dir") + "/";
    String schemaLocation    = DEFAULT_FILE_URL_ROOT + fileName;
    schemaLocation.replaceAll(java.io.File.separator, "/");But when I run this, I get the following exception:
    java.util.regex.PatternSyntaxException: Unexpected internal error near index 1
    ^
            at java.util.regex.Pattern.error(Pattern.java:1489)
            at java.util.regex.Pattern.compile(Pattern.java:1281)
            at java.util.regex.Pattern.<init>(Pattern.java:1030)
            at java.util.regex.Pattern.compile(Pattern.java:777)
            at java.lang.String.replaceAll(String.java:1710)
            at forum.jdom.example.DOMValidator.main(Unknown Source)What's the regex pattern I need to match "\\"? I can't find it. - MOD

    The file seperator is a string which represents the single character which is a backslash (in windows.)
    A single backslash in regex is an escape charater which escapes the next character. But there is no next character in your expression so it doesn't work. You need to replace what you have with the following...
    And if you really insist on using the constant then the expression would be...
    "\\" + java.io.File.separator

  • Regular expressions Pattern

    Hi
    I have to check for for some files in a file system if all the required files exists then next logic continues
    for ex: String pattern = "Hello(A|B|C|D|E|F|G).txt";
    for (int i =0; i < children.length; i++)
    if (Pattern.matches(pattern,children))
    set.add(children[i]);
    above code sets the elements to set if they exists but does't check if some file is missing.
    How do i check my condition for all matches, if not its should exit.
    any help whould be appreciated

    I think this is essentially the same as his other question.
    http://forum.java.sun.com/thread.jspa?threadID=709248

  • Regular Expressions (Pattern/Matcher)

    Hi,
    I have an regex i.e. *Pattern.compile("([0-9]+)D([0-9]+)'?(?:([0-9]+)\")?([NSEW])").*
    It has to exactly match the input e.g *45D15'34"N*
    I need to retrieve the values based on grouping.
    Group1 = 45 (degree value)
    Group2 = 15 (minutes value)
    Group3 = 34 (seconds value) ----> this is a non-capturing group
    Group4 = N (directions)
    The regex works fine for most of longitude/latitude value but I get a StackOverFlow for some. There is a known bug on this [http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=5050507|http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=5050507]
    I was wondering if anyone could suggest a different way of writing the regex above to avoid the stack over flow.
    Thank you in advance

    cienaP wrote:
    Hi,
    I have reposted the topic. Thank you for letting me know, this is my first time.You didn't need to create a [new thread|http://forums.sun.com/thread.jspa?threadID=5416548&messageID=10865976#10865976]! You could just have posted a response containing your regex. I shall lock this thread.

  • Regular expression, Pattern.matcher() method

    Pattern.matcher() method start finding a new match from the end of the previous matcher.
    Can Pattern.matcher() method start finding a new match from the second letter of the previous match?
    For example, this piece of code will give a result as :
    find 1 --- 3
    find 4 --- 6
    +++++++++++++++++++++++++++++++++++++++++++++++++++
              Pattern pattern = Pattern.compile("aaa");
              Matcher matcher = pattern.matcher("aaaaaaa");
              while(matcher.find())
                   int from = matcher.start() + 1;
                   int to = matcher.end();
                   System.out.println("find " + from + " --- " + to);
    ++++++++++++++++++++++++++++++++++++++++++++++++++++
    How can I change the code so the programme can give a result as:
    find 1 --- 3
    find 2 --- 4
    find 3 --- 5
    find 4 --- 6
    find 5 --- 7
    Thanks very much in advance.

    Check out the API there are Two find methods
    public boolean find() - This method starts at the beginning of the input sequence or, if a previous invocation of the method was successful and the matcher has not since been reset, at the first character not matched by the previous match.
    public boolean find(int start) Resets this matcher and then attempts to find the next subsequence of the input sequence that matches the pattern, starting at the specified index.
    so you could do it something like this:
    int startIndex = 0
    while (matcher.find(startIndex)){
      int from = matcher.start() + 1;
      int to = matcher.end();
      System.out.println("find " + from + " --- " + to);
      startIndex = ?????
    }

  • Regular expressions Pattern Matching

    Test Data
    1. xyz<<<testdata>>>123
    2. zzz<<<test<<<data>>>ssf
    3. sss<<<test<data>>>sffsd
    Expected Result
    1. <<<testdata>>>
    2. <<<data>>>
    2. <<<test<data>>>
    Sql Used:
    select REGEXP_SUBSTR('<<<testdata>>>', '(<<<)([^<<<]*)(>>>)', 1, 1) from dual;
    For 1 and 2 it gives me the proper value as expected, but for third test data its not giving any result.
    Can anyone please help me to get this result??

    how about this then:
    WITH t AS (SELECT 'Vendor <<<Acme Co>>>' col1
                 FROM dual
                UNION
               SELECT 'You PO Number <<<12345>>> has been generated.'
                 FROM dual
                UNION
               SELECT '<<<Wile e cyote>>>'
                 FROM dual
    SELECT t.col1
         , REGEXP_SUBSTR(t.col1, '[^a-zA-Z1-9\s]{3}(([a-zA-Z1-9]|\s)+)[^a-zA-Z1-9\s]{3}') new_col1
      FROM t
    COL1                                          NEW_COL1                                                                                                                                                                           
    <<<Wile e cyote>>>                            <<<Wile e cyote>>>                                                                                                                                                                  
    Vendor <<<Acme Co>>>                          <<<Acme Co>>>                                                                                                                                                                       
    You PO Number <<<12345>>> has been generated. <<<12345>>>                                                                                                                                                                         

  • Need a Regular Expression pattern in Java

    Hi,
        Thanks in advance for your help. I have to replace a string if it's not available inside any single quote.
       e.g.
                   name = 'abc12namexyz234' or name='def234namewsr345name' and name like '%ab123name345rt%'
          In the above I've to replace "name" with "ApplicantEntity.name" but I don't want to replace the string that is available inside the single quote i.e. 'abc12namexyz234'. The result after the replacement should be:
                   ApplicantEntity.name= 'abc12namexyz234' or ApplicantEntity.name='def234namewsr345name' and ApplicantEntity.name like '%ab123name345rt%'
         I am trying to find a appropriate regex. But still no success on that. Please Help....
    Thanks,
    Utpal

    .* matches with any character occuring 0 or more times (except newline characters)
    The problem is more likely to be that you really mean any character once or more times (any number of times include 0 times)
    To do this you need .+

  • Regular expressions to parse arithmetic expression

    Hello,
    I would like to parse arithmetic expressions like the following
    "5.2 + cos($PI/2) * -5"where valid expression entities are numbers, operations, variables and paranthesis. Until now I have figured out some regular expressions to match every type of entities that I need, and combine them into one big regex supplied to a pattern and matcher. I will paste some code now to see what I wrote:
    public class RegexTest {
        /** A regular expression matching numeric expressions (floating point numbers) */
        private static final String REGEX_NUMERIC = "(-?[0-9]+([0-9]*|[\\.]?[0-9]+))";
        /** A regular expression matching a valid variable name */
        private static final String REGEX_VARIABLE = "(\\$[a-zA-Z][a-zA-Z0-9]*)";
        /** A regular expression matching a valid operation string */
        public static final String REGEX_OPERATION = "(([a-zA-Z][a-zA-Z0-9]+)|([\\*\\/\\+\\-\\|\\?\\:\\@\\&\\^<>'`=%#]{1,2}))";
        /** A regular expression matching a valid paranthesis */
        private static final String REGEX_PARANTHESIS = "([\\(\\)])";
        public static void main(String[] args) {
            String s = "5.2 + cos($PI/2) * -5".replaceAll(" ", "");
            Pattern p = Pattern.compile(REGEX_OPERATION + "|" + REGEX_NUMERIC + "|" + REGEX_VARIABLE + "|" + REGEX_PARANTHESIS);
            Matcher m = p.matcher(s);
            while (m.find()) {
                System.out.println(m.group());
    }The output is
    5
    2
    +
    cos
    $PI
    2
    5There are a few problems:
    1. It splits "5.2" into "5" and "2" instead of keeping it together (so there might pe a problem with REGEX_NUMERIC although "5.2".matches(REGEX_NUMERIC) returns true)
    2. It interprets "... * -5" as the operation " *- " and the number "5" instead of " * " and "-5".
    Any solution to solve these 2 problems are greately appreciated. Thank you in advance.
    Radu Cosmin.

    cosminr wrote:
    So, I've written some concludent examples and the output after parsing (separated by " , " ):
    String e1 = "abs(-5) + -3";
    // output now: abs , ( , - , 5 , ) , + , - , 3 ,
    // should be:  abs , ( , -5 , ) , + , - , 3 , (Notice the -5)
    I presume that should also be "-3" instead of ["-", "3"].
    String e2 = "sqrt(abs($x=1 + -10))";
    // output now: sqrt , ( , abs , ( , $x , = , 1 , + , - , 10 , ) , ) ,
    // should be:  sqrt , ( , abs , ( , $x , = , 1 , + , -10 , ) , ) ,   (Notice the -10)
    String e3 = "$e * -1 + (2 - sqrt(4))";
    // output now: $e , * , - , 1 , + , ( , 2 , - , sqrt , ( , 4 , ) , ) ,
    // should be:  $e , * , -1 , + , ( , 2 , - , sqrt , ( , 4 , ) , ) ,
    String e4 = "sin($PI/4) - 3";
    // output now: sin , ( , $PI , / , 4 , ) , - , 3 , (This one is correct)
    String e5 = "sin($PI/4) - -3 - (-4)";
    // output now: sin , ( , $PI , / , 4 , ) , - , - , 3 , - , ( , - , 4 , ) ,
    // should be:  sin , ( , $PI , / , 4 , ) , - , -3 , - , ( , -4 , ) ,  (Notice -3 and -4)I hope they are relevant, If not I will supply some more.I made a small change to REGEX_NUMERIC and also put REGEX_NUMERIC at the start of the "complete pattern" when the Matcher is created:
    import java.util.regex.*;
    class Test {
        private static final String REGEX_NUMERIC = "(((?<=[-+*/(])|(?<=^))-)?\\d+(\\.\\d+)?";
        private static final String REGEX_VARIABLE = "\\$[a-zA-Z][a-zA-Z0-9]*";
        public static final String REGEX_OPERATION = "[a-zA-Z][a-zA-Z0-9]+|[-*/+|?:@&^<>'`=%#]";
        private static final String REGEX_PARANTHESIS = "[()]";
        public static void main(String[] args) {
            String[] tests = {
                "abs(-5) + -3",
                "sqrt(abs($x=1 + -10))",
                "$e * -1 + (2 - sqrt(4))",
                "sin($PI/4) - 3",
                "sin($PI/4) - -3 - (-4)",
                "-2-4" // minus sign at the start of the expression
            Pattern p = Pattern.compile(REGEX_NUMERIC + "|" + REGEX_OPERATION + "|" + REGEX_VARIABLE + "|" + REGEX_PARANTHESIS);
            for(String s: tests) {
                s = s.replaceAll("\\s", "");
                Matcher m = p.matcher(s);
                System.out.printf("%-21s-->", s);
                while (m.find()) {
                    System.out.printf("%6s", m.group());
                System.out.println();
    }Note that since Java's regex engine does not support "variable length look behinds", you will need to remove the white spaces from your expression, otherwise REGEX_NUMERIC will go wrong if a String looks like this:
    "1 - - 1"Good luck!

  • Replace period with white space

    I am trying to replace a period in a string with a white space. It does not want to work with a period but works just fine with a comma!
    Please can somebody tell me what I am doing wrong.
    This is the code that I am using:
    System.out.println("Unicode:" + "\u002E");
    CharSequence inputStr = "12.345,67";
    String patternStr = "\u002E";
    String replacementStr = " ";
    //Compile regular expression
    Pattern pattern = Pattern.compile(patternStr);
    //Replace all occurrences of pattern in input
    Matcher matcher = pattern.matcher(inputStr);
    String output = matcher.replaceAll(replacementStr);
    System.out.println("output:" + output);

    As mentioned "." is considered differently in regular expressions. You can change the patterStr to "[.]" and your code will work.
    Since you have used replaceAll() I guess as the size of inputStr varies many thousand seperator can occur. Even then the fix mentioned by me will work
    Thanks
    Arun

Maybe you are looking for

  • Error connecting win 8.1 to sbs 2003 vpn

    When I attempt to connect my (all antivirus off, firewall off, ports forwarded, gre enabled) win 8.1 laptop to my office sbs 2003 machine through a pptp vpn, I get an error. Here is the log: [cmdial32] 21:34:36 11 Custom Action Failed ActionType = Co

  • Error 11503 Whenever I try to download music.

    Can anyone help me? I keep getting error 11503 whenever I try to download music. I updated iTunes, restarted my computer and nothing works. Pleeaasssseeee help. Music is my life.

  • Insert a countdown timer in Keynote?

    Hello all, I'm sure this is an included feature that I just can't find somehow. I would like to insert a countdown timer into a slide. This seems pretty common; I've seen it done a lot. But I just can't find the Insert>timer link, or anything like it

  • SQL Query Executing longer time

    Hi , The below SQL query executing longer time . Please help to Improve the query performance. The query continuously running for more than 24 hours and failing with roolback segment error. Not getting the final output. Most of the tables are having

  • Adding Rank for a Column in SSIS Derived Column

    H I want to add rank to a column in ssis in a derived column actually. Can u please tell how to add it and the syntax for adding it in a derived column.