Format string using Regular Expression

Input string output format...
SELECT q'<select ab_c "ABC", efg "EFG" from dual>' str FROM DUAL
Output:
STR                                 
select ab_c "ABC", efg "EFG" from dual
Required output format using regular expression...
STR                                 
select 'ab_c' "ABC", 'efg' "EFG" from dual

Regular expressions have many limitations as parsing tools, and you didn't specify the rules you wanted. This expression puts quotes around the non blank string before a quoted string:
SELECT regexp_replace(q'<select ab_c "ABC", efg "EFG" from dual>',
                      '([^" ]+)( +"[^ ]*")' , '''\1''\2' ) str FROM DUAL;
STR
select 'ab_c' "ABC", 'efg' "EFG" from dual
{code}
It is not robust - a missing " will confuse it, and you should be using bind variables anyway.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               

Similar Messages

  • Spliting a large string using regular expression which contain special char

    I have huge sting(xml) containing normal character a-z,A-Z and 0-9 as well as special char( <,>,?,&,',",;,/ etc.)
    I need to split this sting where it ends with </document>
    for e.g.
    Original String:
    <document>
    <item>sdf</item>
    <item><text>sd</text</item>
    </document>
    <document>hi</document>
    The above sting has to be splited in to two parts since it is having two document tag.
    Can any body help me to resolve this issue. I can use StringTokenizer,String split method or Regular expression api too.

    manas589 wrote:
    I used DOM and sax parser and got few exception. Again i don't have right to change xml. so i thought to go with RegularExpression or some other way where i can do my job.If the file actually comes in lines like what you posted, you should just be able to compare the contents of each line to see if it contains "</document>" or whatever you're looking for. I wouldn't use regex unless I needed another problem.
    I got excpetion like: Caused by: org.xml.sax.SAXParseException: The entity "nbsp" was referenced, but not declared.
         at org.apache.xerces.util.ErrorHandlerWrapper.createSAXParseException(Unknown Source)
         at org.apache.xerces.util.ErrorHandlerWrapper.fatalError(Unknown Source)
         at org.apache.xerces.impl.XMLErrorReporter.reportError(Unknown Source)So then it isn't even XML.
    Edit: sorry, I just realized why you're considering all of these heavy-duty ideas. It's just that you don't know how to break the string into lines. You do it like this:
    BufferedReader  br = new BufferedReader(new StringReader(theNotXMLString));

  • Filter String using Regular Expression

    Hello,
    I have an application that monitors serial communication between a PC and device.  The message protocol is a byte stream that I convert to a string to parse into pretty messages.  The start of the string is always "10 02", but if the string is preceded with another "10" like this "10 10 02" it is part of a message.  I've been trying to use a regular expression with the Search and Replace VI.  My regex is "[^10]\s10\s02" which almost works but it cuts off part of the message:
    Before:
    10 03 10 02
    After:
    10 0   <= missing the "3"
    10 02
    Here's what I'm doing:
    Any ideas on what I'm missing?  I've attached a simple example.
    Thanks
    Message Edited by Derek Price on 02-14-2008 08:37 PM
    Attachments:
    Filter Beginning Message1.vi ‏14 KB
    FilterMessageRegex1.png ‏7 KB

    Try this approach.
    Do search and replace on '10\s02' and replace with '\r\n10\s20'
    Then do another search and replace on '10\r\n\10\s20'  with '10\s10\s20'
    See attached.
    Randall Pursley
    Attachments:
    Message Filter.PNG ‏18 KB

  • How to Capture Multiple Line String using Regular Expression?

    Hi, 
    I have a simple program like this:
    What I want to accomplish is to capture everything between >>start and >>end using a single Match Regular Expression node. It seems that setting multiple? to True or False does not help.
    I am using LabVIEW 2012.
    If it is impossible to capture it using a single node, that is fine. But I want to make sure that I can make full use of this node without combining serveral others.
    Thank you!
    TailOfGon
    Certified LabVIEW Architect 2013
    Solved!
    Go to Solution.

    Thank you for the fast response! Your solution worked in the example case
    After I saw your post, I was finally able to step forward. But I still wanted to make use of dot notation due to the limitation of characters that match with \w. 
    I made some more modification to your regular expression then now it seems working for all characters:
    >>start((?:\s|.)*)>>end
    Thanks!
    TailOfGon
    Certified LabVIEW Architect 2013

  • Dumbfounded by Scanner processing String using regular expression

    I was reading Bruce Eckel's book when I came across something interesting: extending Scanner with regular expressions. Unfortunately, I was confronted with an issue that doesn't make much sense to me: if the String that I am scanning contains a hyphen, the Scanner doesn't produce anything. As soon as I take it out, it all works like a charm. Here is my example:
    import java.util.Scanner;
    import java.util.regex.*;
    public class StringScan {
    public static void main (String [] args){
         String input = "there's one caveat when scanning with regular expressions";
         Scanner scanner = new Scanner (input);
         String pattern = "[a-z]\\w+";
         while (scanner.hasNext(pattern)){
              scanner.next(pattern);
              MatchResult match = scanner.match();
              String output = match.group();
              System.out.println(output);
    }What could be the reason? I imagined it could be because the hyphen for some reason gets given a special meaning but when I tried escaping it, it still didn't work.

    Thanks for your prompt reply.
    I have figured out what was wrong with my code, by the way. Since a single quote is not a word character, it does not match w+. And as the very first input token does not match, the scanner stops immediately. I rewrote my regex to "[a-z].*" and now it does work.

  • Replace a string using regular expression from powershell

    I want to replace the following:
    'browserName': 'firefox'
    with :
    'browserName': 'chrome'
    then I tried this:
    (get-content $conffile) -replace "^('browserName': ')\S+","browserName': 'chrome' |set-content $conffile
    But nothing happened.
    Could someboby tell me how to write the regular expression here? Thanks a lot.

    Second person today with the same question.
    get-content $conffile |%{$_ -replace "'browserName':\s+'firefox'","'browserName': 'chrome'"  | set-content $conffile
    \_(ツ)_/

  • Filter Strings using regular expressions

    Requirements.
    1.I have a table with different names.
    2.I input a word(string) through a text box.
    3.I filter table using the input string through text box using the code
    ((DefaultRowSorter)table_customer.getRowSorter()).setRowFilter(RowFilter.regexFilter(regex, indices));
    4.regex is obtained as follows.
    String regex = "";
    String text = txtFilterText.getText();
    regex = "^(?i)"text".*"; //for starts with filter
    regex = "." + text + ".";//for contains filter
    regex = "(?i)["text".*]";//for doesnt start with filter
    regex =".*(?i)"text"$";//for end with filter
    I need help for doesnt contain and doesnt end with filters.Plz help me out..
    Anees

    h2. {color:ff0000}Double post{color}
    Reply here: http://forum.java.sun.com/thread.jspa?threadID=5231406

  • Getting non numeric strings using regular expression

    Hi Guys ,
    I  want to get list of string values in table which contains no numeric values  .....
    I have a   string column name A and table name B  .
    I have written following code , but it seems it is incorrect  .
    Plz help me out  .....
    SELECT
    A FROM
    B
    WHERE
    regexp_like(A, '([^[:digit:]])'
    Thanks in advance ....

    96097f0e-f165-463a-a0a2-3d15214c8a3d wrote:
    Hi Guys ,
    I  want to get list of string values in table which contains no numeric values  .....
    I have a   string column name A and table name B  .
    I have written following code , but it seems it is incorrect  .
    Plz help me out  .....
    SELECT
    A FROM
    B
    WHERE
    regexp_like(A, '([^[:digit:]])'
    Thanks in advance ....
    That will give you every one that has at least one non-numeric character, if you want ones which contain no numeric characters then it should be
    regexp_like(A,'^[^0-9]*$')

  • Using Regular Expressions to replace Quotes in Strings

    I am writing a program that generates Java files and there are Strings that are used that contain Quotes. I want to use regular expressions to replace " with \" when it is written to the file. The code I was trying to use was:
    String temp = "\"Hello\" i am a \"variable\"";
    temp = temp.replaceAll("\"","\\\\\"");
    however, this does not work and when i print out the code to the file the resulting code appears as:
    String someVar = ""Hello" i am a "variable"";
    and not as:
    String someVar = "\"Hello\" i am a \"variable\"";
    I am assumming my regular expression is wrong. If it is, could someone explain to me how to fix it so that it will work?
    Thanks in advance.

    Thanks, appearently I'm just doing something weird that I just need to look at a little bit harder.

  • Changeparticular characters in a string by using regular expressions ...

    Hello Everyone,
    I am trying to write a function by using oracles regular expression function REGEXP_REPLACE but I could not succed till now.
    My problem as follows, I have a text in a column for example let say 'sdfsdf Sdfdfs Sdfd' I want replace all s and S characters with X and make the text look like 'XdfXdf XdfdfX Xdfd'.
    Is it possible by using regular expressions in oracle ?
    Can you give me some clues ?
    Thank you

    SSU wrote:
    Hello Everyone,
    I am trying to write a function by using oracles regular expression function REGEXP_REPLACE but I could not succed till now.
    My problem as follows, I have a text in a column for example let say 'sdfsdf Sdfdfs Sdfd' I want replace all s and S characters with X and make the text look like 'XdfXdf XdfdfX Xdfd'.
    Is it possible by using regular expressions in oracle ?
    Can you give me some clues ?
    Thank you
    SQL> SELECT
      2  regexp_replace('sdfsdf Sdfdfs Sdfd','s|S','X') from dual;
    REGEXP_REPLACE('SD
    XdfXdf XdfdfX XdfdRegards,
    Achyut

  • String extract using regular expression

    Hi
    I have text like this "<a>45</a><ct>Hi</ct><R>45 85</R><H>Here</H>" .I want to extract using regular expression or any techniques the text between <R> and </R> also need to replace the space with pipe between 45 and 85 like "45|85"
    Edited by: vishnu prakash on Mar 2, 2012 4:42 AM

    Hi,
    Here's one way:
    REPLACE ( REGEXP_REPLACE ( txt
                    , '.*<R>(.*)</R>.*'
                    , '\1'
         , '|'
         )This assumes there is only one <R> tag in txt.
    Always say which version of Oracle you're using. The expression above will work in Oralce 10 and up, but starting in Oracle 11 you can use REGEXP_SUBSTR rather than the less intuitive REGEXP_REPLACE.
    Edited by: Frank Kulash on Mar 2, 2012 7:48 AM

  • Searching for a substring using Regular Expression

    I have a lengthy String similar to repetetion of the one below
    String str="<option value='116813070'>Something1</option><option value='ABCDEF' selected>Something 2</option>"I need to search for the Sub string "<option value='ABCDEF' selected>" (need to get the starting index of sub string) and but the value ABCDEF can be anything numberic with varying length.
    Is there any way i can do it using regular expressions(I have no other options than regular expression)?
    thanks in advance.

    If you go through the tutorial then you will find this on the second page:
    import java.io.Console;
    import java.util.regex.Pattern;
    import java.util.regex.Matcher;
    public class RegexTestHarness {
        public static void main(String[] args){
            Console console = System.console();
            if (console == null) {
                System.err.println("No console.");
                System.exit(1);
            while (true) {
                Pattern pattern =
                Pattern.compile(console.readLine("%nEnter your regex: "));
                Matcher matcher =
                pattern.matcher(console.readLine("Enter input string to search: "));
                boolean found = false;
                while (matcher.find()) {
                    console.format("I found the text \"%s\" starting at " +
                       "index %d and ending at index %d.%n",
                        matcher.group(), matcher.start(), matcher.end());
                    found = true;
                if(!found){
                    console.format("No match found.%n");
    }It's does everything you need and a bit more. Adapt it to your needs then write a regular expression. Then if you have problems by all means come back and post them up here, but first at least attempt to solve it yourself.

  • Date Validation (yyyy/MM/dd) Using Regular Expression

    Hi Friends,
    I want to validate date entered by user in yyyy/MM/dd format and for this I want to use Regular Expressions only. Also is there any tool that can be used to generate Regular Expression (for Win2000, Win NT)?
    Regards,
    Himanshu Rathore

    try this
    public class Test
         public static void main(String [] args)
              String regex = "\\d{4}/[01]\\d/[0-3]\\d";
              System.out.println("2003/12/11".matches(regex));
              System.out.println("2djd/kj3".matches(regex));
              System.out.println("22/12/12".matches(regex));
              System.out.println("2003/23/05".matches(regex));
              System.out.println("1999/12/51".matches(regex));
              System.out.println("2007/05/07".matches(regex));
    }i'm not able to try on it because i only have jdk1.3.1 installed on my computer and these codes
    required j2sdk1.4

  • 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

  • One for the Tekkies: How to get this output using REGULAR EXPRESSIONS?

    How to get the below output using REGULAR EXPRESSIONS??
    SQL> ed
    Wrote file afiedt.buf
      1* CREATE TABLE cus___addresses    (full_address                   VARCHAR2(200 BYTE))
    SQL> /
    Table created.
    SQL> PROMPT Address Format is: House #/Housename,  street,  City, Zip Code, COUNTRY
    House #/Housename,  street,  City, Zip Code, COUNTRY
    SQL> INSERT INTO cus___addresses VALUES('1, 3rd street, Lansing, MI 49001, USA');
    1 row created.
    SQL> INSERT INTO cus___addresses VALUES('3B, fifth street, Clinton, OK 74103, USA');
    1 row created.
    SQL> INSERT INTO cus___addresses VALUES('Rose Villa, Stanton Grove, Murray, TN 37183, USA');
    1 row created.
    SQL> SELECT * FROM cus___addresses;
    FULL_ADDRESS
    1, 3rd street, Lansing, MI 49001, USA
    3B, fifth street, Clinton, OK 74103, USA
    Rose Villa, Stanton Grove, Murray, TN 37183, USA
    SQL> The REG EXP query shouLd output the ZIP codes: i.e. 49001, 74103, 37183 in 3 rows.Edited by: user12240205 on Jun 18, 2012 3:19 AM

    Hi,
    user12240205 wrote:
    ... Frank, ʃʃp's method, I understand. But your method, although correct, I find it difficult to understand.
    Could you explain how you did this?? What does '.*(\d{5})\D*' and '\1' mean???
    Your method is better because it uses only ONE reg expression function. ʃʃp's uses 2.In Oracle 10.2 (I believe) and higher, '\d' is equivalent to '[[:digit:]]', and '\D' is equivalent to '[^[:digit:]]'. I find '\d' and '\D' easier to type, but there's nothing wrong with using '[[:digit:]]' and '[^[:digit:]]'.
    '.*' means "0 or more of any character".
    '\D*' means "0 or more non-digits".
    The whole expression, '.*(\d{5})\D*' means:
    a. 0 or more characters (any characters)
    b. 5 digits
    c. 0 or more non-digits.
    '\1' is a Backreference . It means the sub-string that matched the pattern after the 1st '(', up to (but not including) its matching ')'. In this case, that means the sub-string that matched '\d{5}', or b. using the explanation immediately above.
    So the entire REGEXP_REPLACE call means "When you see a sub-string consisting of a., follwed immediately by b., followed immedately by c., replace that sub-string with b. alone."

Maybe you are looking for

  • Path to mask using JS

    How can I convert a path to a mask using JS ? 98% of my images contains a path and would like to convert the selected path to a mask. Thanks in advance

  • Error when trying to use FaceTime

    facetime previously worked fine on my daughter's macbook pro with lion, now when opening facetime it asks for the password, flashes verified for a second, then gives this error: 'the server encountered an error processing registration, please try aga

  • Can Anyone tell me the problems of iphone 5

    Hello Can Anyone one tell me the problems in iphone 5 when released and what have been fixed ? Thanks In Advance.

  • RAID Mirror failed - help interpreting error code?

    Hi all - I run the primary disk mirrored on my Dual 2.0 Xserver (10.4.7) using Apple's software RAID. No problems for two years, but found this am a warning on System Monitor, showing my RAID as degraded - one disk had dropped out of the set. All fun

  • Help with One to Many Formula

    Post Author: Don CA Forum: Formula Hello Everyone, I need some assistance.  I have a one to many relationship... Example... TABLE 1 FieldName = RecordID DATA, RecordID= 10 TABLE 2, linked one to many to RecordID in TABLE1 FieldNames = RecordID, Name,