Regular Expression / String question?

Ok, So I want to do a String.split and split by a
(.*)But since parenthese's are regex, how would I search for that without messing up the call?

CButz wrote:
so
\(.*\)should work?Yup.
But note that if the regex is Java string literal--that is, in your source code in double quotes, as opposed to being read from user input or a config file, you'll need to double the backslash.
"\\(.*\\)"Also, note that that regex will match (abc)def(ghi) as one group. The abc)def(ghi will match the .*

Similar Messages

  • Jakarta regular expression package question

    hi everyone,
    I'm trying to figure out how to use the jakarta regular expression package to return an array of matches ?
    For example let's say i had StringBuffer object consisting of say 30,000 characters and I needed to return all the matches that started with < and ended with > (like all the xml tags )
    anybody know how I could use the package to return an array of matches ?
    thanks in advance
    stev

    okay -- perhaps i should maybe rephrase my question for this package -- perhaps it is more popular ?
    anyone know how i can pass a regular expression to it and retrieve a String array
    In my specific situation , I have a StringBuffer with about 10,000 characters in it ?
    any ideas how i would use regular expressions to look through this Stringbuffer object
    thanks
    stephen

  • Regular expressions string length

    Hi
    New to regular expressions and trying to create an expressions that will test if a string does not contain any number and is less than 20 characters long! This works for determining if it has any numbers in it, but how do I specify that it can be between 1 and 20 characters in length? The + means >1 but how do I cap it?
    Pattern p = Pattern.compile("[a-zA-Z]+");
             Matcher m = p.matcher(value);
             if (m.matches()) System.out.println("yes");I have tried [a-zA-Z]+{5} but that doesn't work. Any handy hints... can't find much stuff that is applicable via google, frustrating.
    Thanks.

    Looce wrote:
    Pattern p = Pattern.compile("[a-zA-Z]{1,20}"); // repetition totally replaces +, doesn't coexist with it
    Matcher m = p.matcher(value);
    if (m.matches()) System.out.println("yes");
    Note that the following code can be shortened by doing:
    if(value.matches("[a-zA-Z]{1,20}")) { /* ... */ }

  • Regular Expression string

    Hi,
    I'm looking for a single regular expression.
    E.g. My string is 'get data for comparison'. So when using regular expression as \Wcompar then it returns true.
    But I dont want it to return true for string 'get data for comparable' with the same regular epression.
    Here, I do not want string comparable and rest all the forms of compar to be selected.

    It's not really clear what you need, perhaps something like this?
    -- data:
    with t as
    select 'get data for comparison' txt from dual union all
    select 'get date for compar xxxxx' txt from dual union all
    select 'compar xx' txt from dual union all
    select 'compare xx' txt from dual union all
    select 'xx compar' txt from dual union all
    select 'xx compare' txt from dual
    -- query:
    select txt from t
    where regexp_like(txt,'(\W|^)compar(\W|$)');
    TXT
    get date for compar xxxxx
    compar xx
    xx compar(\W|^) means word-limit or start of the string
    (\W|$) means word-limit or end of the string
    Edited by: hm on 21.12.2011 05:29

  • Regular expression usage question

    Hi there.
    I have a 200 bytes EBCDIC variable record which I need to break down into fields. Fields are positional and are either text, binary numbers, packed-decimal and 64bytes long numbers.
    My question is. Can regular expression handle this complex data.
    I want to isolate each field into their corresponding format. EBCDIC into ASCII text, binary into java Integer and so on.
    The reason for using reqular expression is because the record format could change and regular expression would be easier to modify without having to change the code.
    Your words of advice are highly appreciated.
    Please advice.
    Regards,
    Ulises

    Regular expressions? I don't think so.
    If you have a situation where positions 1-3 might be a binary number like client number, and the format might change so it moves to positions 12-14, then you could certainly write a record-format class to encapsulate that sort of information. In fact that would be a very good idea. But I can't imagine how a regular expression would help in getting a number out of three bytes, for example.

  • Command line style regular expression string parser

    Hi people,
    I am currently working on a program where I need to parse a file (or any input stream) line by line. I then need to parse every line for arguments. Each line is formatted similar to how arguments are passed to the command line. The regular expression needs to split every line by any encountered whitespace, but needs to be able to retain any whitespace within double quotes (i.e. "some spaced text here"). Arguments can be numbers, booleans and (quoted) strings. Quoted strings must also be able to have escaped quotes in it (as below). The quotes for the quoted string (the outer ones, obviously not the escaped ones) do not necessarily have to be retained.
    An example input line:
    arg1 arg2 "arg 3" "arg 4" 987 arg6 "arg \"arg \"arg 7"
    Desired example output:
    arg1
    arg2
    arg 3
    arg 4
    987
    arg6
    arg "arg "arg 7
    After the input line has been split up the program will handle any parsing (i.e. numbers, booleans, etc.). The program currently uses a simple for loop to iterate over all characters in the line and splits it up appropriately by checking every character. However, if this can be done automatically by using a regular expression passed to String.split() (or with some use of the regex package), it would remove quite a bit of redunant code and make the program that much more maintainable.
    I do not have much experience with regular expressions since I have never really had the need to use them, but if they can work in this case it would be great.
    Thanks in advance for any help.

    Almost any parsing problem can be solved if you throw a big enough and ugly enough regex at it, or so I'm told.
    I think what you are doing is also amenable to java.io.StreamTokenizer:
    import java.io.*;
    import static java.io.StreamTokenizer.*;
    public class StreamTokenizerExample {
        public static void main(String[] args) throws IOException {
            StringReader input = new StringReader("arg1 arg2 \"arg 3\" \"arg 4\" 987 arg6 \"arg \\\"arg\\\" arg 7\"\nnextline");
            StreamTokenizer in = new StreamTokenizer(input);
            in.eolIsSignificant(true);
            for(int ttype; (ttype = in.nextToken()) != TT_EOF; ) {
                switch (ttype) {
                    case TT_WORD:
                        System.out.println("String[" + in.sval + "]");
                        break;
                    case TT_NUMBER:
                        System.out.println("number[" + in.nval + "]");
                        break;
                    case TT_EOL:
                        System.out.println("[EOL]");
                        break;
                    case '"':
                        System.out.println("quoted[" + in.sval + "]");
                        break;
                    default:
                        System.out.println("unexpected " + ttype);
    {code}                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   

  • 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 Expression / String matching other than equality check with groovy.

    I want to have a string comparison operation using a groovy expression.
    Not an equality check, but something like
    #{variableName like 'IC%'?'true':'false'}
    I saw some operators like
    #{bindings.gradeName.inputValue =~ 'IC'?true:false}
    But it doesnt seem to be working.
    Is it possible with groovy?
    Arun

    Hi
    nything will do.
    And not using bean.
    This expression has to added in an AMX page (ADF Mobile
    It depends on where you want to execute this. Groovy in a validation script would be different to EL in a page as they would be in different stages in the lifecycle.
    Now for ADF MObile this may be different, I dont know.'
    If its to be part of a validation then you can call the source methods in groovy and do the comparison.
    from a validator for instance:
    source.getGradeName() returns the grade name before commiting.
    String enteredgrade = source.getGradeName();
    if (enteredgrade.index(0).equals('I') and enteredgrade(1).equals('C'){
    do what you want here. i.e. return true or false etc.
    el in a page would look entirly different but you can implement this same functionality in a bean, access it via the accessors and then return true or false.
    if you are already using EL and you managed to link your page with JSTL, you will have to define the access variable in the header section inorder to access it and then use "startswith", though I havent tried it this way.

  • Regular Expressions / String Match

    Hi Everyone,
    thankx for reading this
    I'm programing a oracle form which at one part read's the full path into a file (a text file) and place's it on a VARCHAR2 field. This is the path into the file and not the file data.
    I would like to match this string (the file path) to a another one (in order to know if the file name respect some rules). This is a very easy thing to do under Perl / PHP and other languages. But how can i do this under PL/SQL ?
    I have search but failed to find any clue.
    Any pointers would be great, or a small example
    Thankx Stay Happy
    Miguel Angelo

    Hi,
    you can use INSTR and SUBSTR for String comparison
    INSTR(char1,char2) shows you the first occurence of char2 in char1
    INSTR(char1,char2,n,m)
    shows the m'th occurence of char2 in char1 starting on n
    Frank

  • Regular Expression - Quick Question

    Hi Experts,
    SELECT REGEXP_SUBSTR('123@123','[^@@]+',1,1) FROM DUAL;
    Result:
    123 -- Wrong one.
    SELECT REGEXP_SUBSTR('123@@123','[^@@]+',1,1) FROM DUAL;
    Result:
    123 -- Right one.How can i achieve this?
    Thanks,

    michaels2 wrote:
    SQL> with t as
    select '123@123' str from dual union
    select '123@@123'  from dual
    select str, regexp_replace(str,'@@.*') x  from t
    STR      X      
    123@123  123@123
    123@@123 123    
    2 rows selected.
    No michael, he needs something like:
    SQL> with t as
      2  (
      3   select '123@@456@@789@@000' str from dual
      4  )
      5  select regexp_substr(replace(str,'@@','#'),'[^#]+',1,level)
      6  from t
      7  connect by level<=(length(str)-length(replace(str,'@@','')))/2+1;
    REGEXP_SUBSTR(R
    123
    456
    789
    000Because he has to extract tokens separated by @@, but I can't find a neat solution...
    seems like Oracle is ignoring the grouping operator:
    SQL> with t as
      2  (select '123@123' str from dual union
      3  select '123@@123'  from dual)
      4  select str, regexp_substr(str,'[^(@@)]+')
      5  from t;
    STR      REGEXP_S
    123@123  123
    123@@123 123In the first row it should extract the full string, not just the first three characters...
    Max
    [My Italian Oracle blog| http://oracleitalia.wordpress.com/2010/02/07/aggiornare-una-tabella-con-listruzione-merge/]

  • Regular Expression to capture user's input string

    I am writing a helper class to split user input string into String array according to the following pattern:
    import java.util.regex.Matcher;
    import java.util.regex.Pattern;
    public class TestDelimiter {
    static Pattern p = Pattern.compile("(.*?)[,;]{1}");
    static Matcher m;
        public static void main(String[] args) {
            String input = "AAA111111,BBB222222;CCC333333";
            m = p.matcher(input);
            while(m.find()){
                String output = m.group(1);
                System.out.println(output);
    }Output:
    AAA111111
    BBB222222My question is, how can I modify the regular expression string so that the CCC333333 (last element) can also be included?

    roamer wrote:
    Ok, let's don't argue on this point.Who's arguing?
    I think I got the answer. For simplicity, I can just manually add a ";" or "," string after each user input. Just like:You never said anything about that before. You asked how to split "AAA111,BBB222;CCC333" into its components and you were given a correct answer.
    Maybe you should rephrase your question including this added requirement. Do you need to have the separator included in the final outputs or why do you want to suddenly add things to user input?

  • Regular expression for 2nd occurance of a substring in a string

    Hi,
    1)
    i want to find the second occurrence of a substring in a string with regular expression so that i can modify that only.
    Ex: i have a string like ---> axe,afn,sdk,jdi,afn,mki,mki
    in this i want the second occurance of afn and change that one only...
    which regular expression i have to use...
    Note that ...i have to use regular expression only....no string manipulation methods...(strictly)
    2)
    How can i apply the multiple regular expressions multiple times on a single string ..i.e in the above instance i have to apply the same 2nd occurrence logic for
    substring mki also. for this i have to use a single regular expression string that contains validations for both the sub strings mki and afn.
    Thanks in advance,
    Venkat

    javafreak666 wrote:
    Hi,
    1)
    i want to find the second occurrence of a substring in a string with regular expression so that i can modify that only.
    Ex: i have a string like ---> axe,afn,sdk,jdi,afn,mki,mki
    in this i want the second occurance of afn and change that one only...
    which regular expression i have to use...
    Note that ...i have to use regular expression only....no string manipulation methods...(strictly)
    2)
    How can i apply the multiple regular expressions multiple times on a single string ..i.e in the above instance i have to apply the same 2nd occurrence logic for
    substring mki also. for this i have to use a single regular expression string that contains validations for both the sub strings mki and afn.
    Thanks in advance,
    VenkatWhat do you mean by using a regex to get the index of a second substring? There is not method in Java which uses regex to et the index of a substring.
    There are various indexOf(...) methods for this:
    String text = "axe,afn,sdk,jdi,afn,mki,mki";
    String target = "afn";
    int second = text.indexOf(target, text.indexOf(target)+1);
    System.out.println("second="+second);Of course you can find the index of a group like this:
    Matcher m = Pattern.compile(target+".*?("+target+")").matcher(text);
    System.out.println(m.find() ? "index="+m.start(1) : "nothing found");but there is not single method that handles this: you'll have to call the find() and then the start(...) method on the Matcher instance, so the indexOf(...) approach is the favourable one, IMO.

  • Looping through files with Regular expressions ?

    Hi,
    My Question is:
    if i have the following Regular Expression,
    String regrex = "tree\\s\\w{1,4}.+\\s=\\s(.*;)";
    The file in which i am looking for the string has multiple entries, is it
    possible to do another regular expression on the captured group (.*;)
    which is in the original Regular expression ?
    The text that is captured by the RE is of the type "(1,(2,((3,5),4)));" for
    each entry, and different entries in the file have slightly different syntax
    is it possible to loop through the file and first of all check for the presence
    of the original RE in each entry of the file
    and then secondly, check for the presence of another RE on the captured group?
    [ e.g. to check for something like, if the captured group has a 1 followed by a 3
    followed by a 5 followed by a and so on ].
    Thanks Very much for any help, i've been struggling with this for a while!!
    Much appreciated
    The code that i have so far is as follows:
    import java.util.*;
    import java.util.regex.*;
    import java.io.*;
    import java.lang.*;
    import javax.swing.*;
    public class ExpressReg {
    public String Edit;
    public ExpressReg(String editorEx){
    Edit = editorEx; // Edit = JTextArea
    String regrex = "tree\\s\\w{1,4}.+\\s=\\s(.*;)";
    //String regrex1 = "(.*;)";
    Pattern p = Pattern.compile(regrex);
    Matcher m = p.matcher(editorEx); // matcher can have more than one argument!
    boolean result = m.find();
    if(result){                           
    JOptionPane.showMessageDialog(null, "String Present in Editor");
    else if(!result){
    JOptionPane.showMessageDialog(null, "String Not Present In Editor");

    if i have the following Regular Expression,
    String regrex = "tree\\s\\w{1,4}.+\\s=\\s(.*;)";
    The file in which i am looking for the string has multiple entries, is it
    possible to do another regular expression on the captured group (.*;)
    which is in the original Regular expression ?Yes, the capturing group is $1 (the only one) referenced in source code as m.group(1).
    m.group() will return entire matching.
    simply use this way:
    String result = m.group(1);
    // your stuff: could be another validation
    The text that is captured by the RE is of the type "(1,(2,((3,5),4)));" for
    each entry, and different entries in the file have slightly different syntax
    is it possible to loop through the file and first of all check for the presence
    of the original RE in each entry of the file
    and then secondly, check for the presence of another RE on the captured group?Again "Yes", no limits!
    Don't need to create another Matcher, just use m.reset(anotherSourceString)..loop using the same pattern.
    Note: Take care with ".*" because regex nature is "greedy", be more specific, eg.: "\\d" just matches digits (0-9).
    Can you give us some sample of "slight difference" ?

  • How to define a regular expression using  regular expressions

    Hi,
    I am looking for some regular expression pattern which will identify a regular expression.
    Also, is it possible to know how does the compile method of Pattern class in java.util.regex package work when it is given a String containing a regex. ie. is there any mechanism to validate regular expression using regular expression pattern.
    Regards,
    Abhisek

    I am looking for some regular expression pattern which will identify a regular
    expression. Also, is it possible to know how does the compile method of
    Pattern class in java.util.regex package work when it is given a String
    containing a regex. ie. is there any mechanism to validate regular
    expression using regular expression pattern.It is impossble to recognize an (in)valid regular expression string using a
    regular expression. Google for 'pumping lemma' for a formal proof.
    kind regards,
    Jos

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

Maybe you are looking for

  • SelectionScreen - DynPro sequence/hopping

    Hej together, I'm stuck on the following: I do have to rewrite an old inconsistency report to use new UI techniques instead of old lists. At first a selection-screen is created. The criteria of this screen is checked using the event AT SELECTION-SCRE

  • Display header text and item text in smartform

    Hi all, I have to display header text and item text of PO in smartform BBP_SUSPO. So how to do that.. I have to use 'read_text' Fm or directly i can get values from any table ??? Thanks in  advance. Regards, Anagha Deshmukh

  • 2010 Tax Forms print errors in Reader

    All of the tax forms I've downloaded from the IRS web site print with errors --- lines, garbage characters, etc when printing from Reader (latest version) on OSX.  If I open the .pdf files with Photoshop and print them, they come out just fine.  Prin

  • Encountered ORA-01722 Invalid number when query doc from  view

    Hi all, I have the following view. Select item, to_number(wo) a from tablea union all select item, wo from tablebb The problem rise ( ORA-01722) when I try to search for wo from the view after the view has been created. The wo field in tablea was var

  • PHP Variable Capitalize

    Okay so I have a php variable, and I set it to "Home", then when I call it though I get a result of "home". How do I make the variable retain the capital H?