Replace with complex logical regular expression

What is the best way to replace the input string with the following rules:

1. all sequential occurrences of two or several hyphen-symbols (symbol: '-') must be replaced by a hyphen. So, if the input string is "-" then output should be ' - '. If the entry was "a-a-a", then output remains what it is.
2. all occurrences of two or more space sequential symbols (symbol: "" "") must be replaced by a space. If the input string is "" then exit must therefore «» If the entry was "one one one" then exit remains as it is.
3. once the 1-2 rules are applied after the rule applies: all occurrences of the space symbol followed immediately after the hyphen-symbol or vice verse-trait of Union followed by space should be replaced with the hyphen. So, if the input string is "»" or "-" then the output should be "«»". " If the entry was 'a - a', then output remains what it is.
4. once the 1-2-3 rules are apllied follows the rule should be applied: String beginning and symbol of the end can only be space or hyphen. Therefore, if the input string is 'a' or '-a ' or 'a' or 'a-' and then the output should be 'a '.

All rules 1 to 4 must be apllied to the input string.
Example of replacement logic:
entry: 'an aa - ss-ee -'
output: "an aa-ss-ee."

I think that I should use the function "regexp_replace" somehow.
This solution below does not work because it produces two consecuent dashes "-":
with t as(
 select 'a   aa ---- ss-ee -' str from dual
)
select regexp_replace(str, '--','-') a from t;
/*a   aa -- ss-ee -*/
Can you suggest a regular expression for it?

Maybe something like this:

SQL> with t as(
  2   select 'a   aa ---- ss-ee -' str from dual
  3  )
  4  select rtrim(rtrim(ltrim(ltrim(
  5         regexp_replace(regexp_replace(str, '(-| )\1+','\1'), '( -|- )', ' ')
  6         , '-') , ' '), '-'), ' ') a from t;

A
-----------
a aa  ss-ee

Notice how it ends with 2 spaces in a row in the Middle, because there was a segment with '-' (space followed by '-') which was replaced by a space.

Tags: Database

Similar Questions

  • Need help with a simple regular expression replacement

    Hello everyone,

    He comes to the table that I have to work with.
    CREATE TABLE "TBL_ACCOMMODATION" ("ACCOMMODATION_ID" NUMBER, "HOTEL_NAME" VARCHAR2(100), "ADDRESS" VARCHAR2(200), "LOCATION" VARCHAR2(100), "PHONE" VARCHAR2(50), "EMAIL_ADDRESS" VARCHAR2(60), "CONTACT_PERSON" VARCHAR2(60), "STATUS" CHAR(1), "CREATED_BY" VARCHAR2(10), "CREATED_DATE" DATE, "MODIFIED_BY" VARCHAR2(10), "MODIFIED_DATE" DATE, "MOBILE" VARCHAR2(50))
    REM INSERTING into TBL_ACCOMMODATION
    Insert into TBL_ACCOMMODATION (ACCOMMODATION_ID,HOTEL_NAME,ADDRESS,LOCATION,PHONE,EMAIL_ADDRESS,CONTACT_PERSON,STATUS,CREATED_BY,CREATED_DATE,MODIFIED_BY,MODIFIED_DATE,MOBILE) values (147,'Testt','Auckalnd','Henderson','565756776','[email protected]','Jasmine','A',null,null,'JEEJJ',to_date('23/10/12','DD/MM/RR'),null);
    Insert into TBL_ACCOMMODATION (ACCOMMODATION_ID,HOTEL_NAME,ADDRESS,LOCATION,PHONE,EMAIL_ADDRESS,CONTACT_PERSON,STATUS,CREATED_BY,CREATED_DATE,MODIFIED_BY,MODIFIED_DATE,MOBILE) values (129,'Kirby Hotel','25A Aitken Street
    Wellington','Wellington','04 918 8513','[email protected]','Deahdoow Maharg','A',null,null,'LEAN',to_date('14/02/13','DD/MM/RR'),'027 356 4333');
    Insert into TBL_ACCOMMODATION (ACCOMMODATION_ID,HOTEL_NAME,ADDRESS,LOCATION,PHONE,EMAIL_ADDRESS,CONTACT_PERSON,STATUS,CREATED_BY,CREATED_DATE,MODIFIED_BY,MODIFIED_DATE,MOBILE) values (167,'Avenue ee','10 Wellington Street
    Wellington','Wellington','4444444','[email protected]','James','A',null,null,'LEAN',to_date('21/02/13','DD/MM/RR'),null);
    Insert into TBL_ACCOMMODATION (ACCOMMODATION_ID,HOTEL_NAME,ADDRESS,LOCATION,PHONE,EMAIL_ADDRESS,CONTACT_PERSON,STATUS,CREATED_BY,CREATED_DATE,MODIFIED_BY,MODIFIED_DATE,MOBILE) values (185,'Quadrant Hotel','10 Waterloo Quadrant,
     Auckland','Auckland','9555555','[email protected]','Quentin QQ','A',null,null,'LEAN',to_date('04/03/13','DD/MM/RR'),null);
    My select statement is
    SELECT acc.hotel_name || '(' || replace(replace(acc.address,chr(13),', '),',,',',') || ')'
    FROM TBL_ACCOMMODATION acc
      inner join ijs_seminar s ON acc.accommodation_id = s.accommodation_id 
    where s.seminar_id = :P27_SEMINAR_ID
    I have to use the function replace twice. One is to replace the Chr (13) a comma is second band a comma where there are two commas.

    I don't know much about regular expressions.
    If someone can show me a better way to handle this using the regular expression rather than a heavy means above.

    Thanks in advance
    Ann

    Hi, Ann.

    Ann586341 wrote:
    Hello everyone,

    He comes to the table that I have to work with.

    CREATE TABLE "TBL_ACCOMMODATION" ("ACCOMMODATION_ID" NUMBER, "HOTEL_NAME" VARCHAR2(100), "ADDRESS" VARCHAR2(200), "LOCATION" VARCHAR2(100), "PHONE" VARCHAR2(50), "EMAIL_ADDRESS" VARCHAR2(60), "CONTACT_PERSON" VARCHAR2(60), "STATUS" CHAR(1), "CREATED_BY" VARCHAR2(10), "CREATED_DATE" DATE, "MODIFIED_BY" VARCHAR2(10), "MODIFIED_DATE" DATE, "MOBILE" VARCHAR2(50))
    REM INSERTING into TBL_ACCOMMODATION
    Insert into TBL_ACCOMMODATION (ACCOMMODATION_ID,HOTEL_NAME,ADDRESS,LOCATION,PHONE,EMAIL_ADDRESS,CONTACT_PERSON,STATUS,CREATED_BY,CREATED_DATE,MODIFIED_BY,MODIFIED_DATE,MOBILE) values (147,'Testt','Auckalnd','Henderson','565756776','[email protected]','Jasmine','A',null,null,'JEEJJ',to_date('23/10/12','DD/MM/RR'),null);
    ...
    

    Thanks for posting the sample data. You post the results, but the validation of the existing query which I suppose, produced good results. I can't run this query, because it requires the ijs_seminar table and a connection variable that also has no post, but I can just comment on the join and the WHERE clause.

    Is this really the best set of sample data for this question? This problem involves Chr (13) and repeated commas, but I don't see any s CHR (13) or repeated commas in the sample data. In addition, it seems that there are a lot of columns that play no role in this issue and just to make things difficult to read.

    My select statement is

    SELECT acc.hotel_name || '(' || replace(replace(acc.address,chr(13),', '),',,',',') || ')'
    FROM TBL_ACCOMMODATION acc
    inner join ijs_seminar s ON acc.accommodation_id = s.accommodation_id
    where s.seminar_id = :P27_SEMINAR_ID
    

    I have to use the function replace twice. We need to replace the Chr (13) by a comma,.

    As posted, inside REPLACE replaces Chr (13) with a comma and a space which could be important if you then pick up consecutive commas.

    second is the band a comma where there are two commas.

    I don't know much about regular expressions.
    If someone can show me a better way to handle this using the regular expression rather than a heavy means above.

    Assuming you want to replace Chr (13) with just a comma, then an equivalent would be:

    SELECT    acc.hotel_name || '('
                      || REGEXP_REPLACE ( acc.address
                                          , '[,' || CHR (13) || ']{1,2}'
                                , ','
                                )
                    || ')'            AS h
    FROM        tbl_accommodation acc
    INNER JOIN  ijs_seminar       s   ON   acc.accommodation_id = s.accommodation_id
    WHERE s.seminar_id = :P27_SEMINAR_ID
    ;
    

    In the argument to REGEXP_REPLACE 2nd

    [xy]{1,2}
    

    medium 1 to 2 characters of set of x and y. This could be
    x or
    there or it could be 2 characters
    XY or the other way
    YX or it could be the same characters 2
    XX or
    YY

    REGEXP_REPLACE is slower that REPLACE. Even if your original expression is longer, it may be more effective. (Performance may be not a problem in this case.)

  • Find and replace - regular expression to help?

    How can I find ' <!-* PAGE footer AREA *-> "and replace it and evething after with my new coding of footer on all my pages.

    The problem is:

    The code in my existing page footer area is not the same on all pages.

    I want to replace it with a new one on all pages

    I watch the operators of regular expressions, not found patern that works...

    Thanks in advence for your help

    The following regular expression is the and everything up, but not including the closing tag:

    [\s\S]+(?=<\/body>)
    

    Use it in the search field and put the comment and the code to the footer in the field replace. Select use regular expression.

    Always make a backup before using a regular expression on many pages.

  • Form validation helps with the regular Expression [a-zA-Z]

    I'm trying to use the regular expression [a-zA-Z] to allow only upper or lowercase WITHOUT SPACES. With the help of [a-zA-Z] allows space and numbers.

    Could someone give me a point in the right direction?

    Thank you!

    RGNelson wrote:

    I'm trying to use the regular expression [a-zA-Z] to allow only upper or lowercase WITHOUT SPACES. With the help of [a-zA-Z] allows space and numbers.

    Could someone give me a point in the right direction?

    Please try with the following regular expression, which should work for text entry fields 'a line' well standard.

    ^ [A-Za-z] + $

    See you soon,.

    Günter

  • How to capture multiple line String using regular expressions?

    Hello

    I have a simple program like this:

    What I want to accomplish is to capture everything between > start and > to end with a single regular expression matching node. It seems that the definition of multiples? true or False does not help.

    I'm using LabVIEW 2012.

    If it is impossible to capture using a single node, that's fine. But I want to assure you that I can make full use of this node without combining several others.

    Thank you!

    > start([\w\s]*) > end

    A point matches any character except line break characters.  You have two of them.

  • Set-search-data-hiding-rule-prop-dn-regular-expressions setting targets

    Hello. I'm trying to put in place a masking rule 6.3 DPS data. I want to have the data masking rule apply to any DN that ends with o = ny, c = us. I tried to adjust the target-dn-regular-expressions to ' o = ny, c = us$ '. I thought that if DPS used regular expression match, the $ at the end should serve as an anchor for this channel. But the rule is not fired. I proved it by assigning the expression regular votes to zero and then the target dns to exactly match the dn I'm back. Anyone know what I need to put in the phrase correspond to what I want? Thank you

    Hello

    Regular expressions must comply with the Java regular expression specificationa, available on [http://download.oracle.com/javase/6/docs/api/java/util/regex/Pattern.html]

    For example, the regular expression (. *) c = us, o = ny filters out all o = ny, c = us tree.

    Hope this helps

    Sylvain

  • Grouping and backreferences with regular expressions on the window to replace the text

    I'm really appreciate the inclusion of regular Expressions in the search and replace functionality. One thing miss me that East of backreferences in the replacement expression. For example, in unix tools vi or sed, I could do something like this:
     s/\(firstPart\) \(secondPart\) \(oldThirdPart\)/\2 \1 newThirdPart/g 
    that allow me to switch the places of first and secondPart and substitute totally thirdPart. If grouping and backreferences are already present in the window replace text, how do you properly call them?

    Published by: Justin.Warwick on August 23, 2011 08:26

    You can vote on the request for this to the exchange of SQL Developer, to add weight to the implementation as soon as possible: https://apex.oracle.com/pls/apex/f?p=43135:7:3693861354483465:NO:RP, 7:P7_ID:16761

    Kind regards
    K.

  • Regular expression find and replace with wildcards

    Hello!

    For the world of me, I can't understand the right way to do it.

    I made a list of family names, first names. I want to have a different style than the first name, last name.

    So that's what I have now:

    < b > AAGAARD, TODD, s. < /b > < br >
    AAMOT, KARI, < b > < /b > < br >
    < b > AARON, MARJORIE, c. < /b > < br >

    and that's what I have:

    < span class = "Name" > AAGAARD </span > < span class = "FirstName" >, TODD, s. </span > < br / >

    < span class = "Name" > AAMOT </span > < span class = "FirstName" >, KARI, </span > < br / >

    < span class = "Name" > AARON </span > < span class = "FirstName" >, MARJORIE, C. </span > < br / >

    Any ideas?

    Thank you!

    Make a backup first.

    In the use of the search box:

    (\w+),\s+([^<]+)<\/b>\s*

    In the field using replace:

    $1 $2

    Select use regular expression. Light blue paper touch, then click on replace all.

  • I know by logic box to get the output using the regular expression?

    Hello

    I am now only study the notion of Regexp. I had seen the best of Mr. Blus. Can I know how it works. I need this logic of the functionality of wildcards in sting matching Regexp (wildcard string Matching).
    SQL> ed
    Wrote file afiedt.buf
     
      1  WITH test_data AS (
      2  SELECT 'c:\temp\folderA\fileA.txt' t FROM DUAL UNION ALL
      3  SELECT 'c:\temp\fileA.txt' t FROM DUAL UNION ALL
      4  SELECT '\\mymachine\A\fileB.txt' t FROM DUAL UNION ALL
      5  SELECT '\\mymachine\A\B\fileB.txt' t FROM DUAL UNION ALL
      6  SELECT '\\mymachine\A\B\C\image.jpg' t FROM DUAL UNION ALL
      7  SELECT '\\mymachine\A\B\C\D\music.mpg' t FROM DUAL UNION ALL
      8  SELECT 'c:\myfolder\folderD\folderE\4969-A.txt' t FROM DUAL
      9  )
     10  select regexp_replace(t, '^.*[\]([^\]*)[\][^\]*$','\1')
     11* from test_data
    SQL> / 
     
    REGEXP_REPLACE(T,'^.*[\]([^\]*)[\][^\]*$','\1')
    ------------------------------------------------------------------
    folderA
    temp
    A
    B
    C
    D
    folderE
     
    7 rows selected.
     
    SQL>
    Please help me in this matter.

    Iqbal

    Sabrina wrote:
    One last question what is the average of

    The final "\1" in

     select regexp_replace(t, '^.*[\]([^\]*)[\][^\]*$','\1') 
    

    Iqbal

    It is a reference.

    See here:
    http://download.Oracle.com/docs/CD/B19306_01/AppDev.102/b14251/adfns_regexp.htm#CHDHCIGH

    and in the middle of the table here:
    http://download.Oracle.com/docs/CD/B19306_01/AppDev.102/b14251/adfns_regexp.htm#CHDIEGEI

    Matches the nth previous subexpression, in other words, either grouped in parentheses, where n is an integer between 1 and 9. The parentheses cause > an expression be remembered; a backreference refers to him. A backreference account subexpressions from left to right, starting with the opening > bracket of each subexpression preceding. The expression is not valid if the source string contains less than n subexpressions preceding the \n.

    Oracle supports the expression of backreference in the regular expression pattern and the replacement of the REGEXP_REPLACE function string.
    The expression (abc: def) matches the strings abcxyabc and defxydef xy\1, but does not abcxydef or abcxy.

    A backreference allows you to search for a string repeated without knowing the actual string advance. For example, the expression ^(.*) \1$ > matches a line consisting of two adjacent instances of the same string.

    As explained in table 4-2, backreferences store sub-expressions matched in a temporary buffer, which allows to reposition the characters. You access the pads with the notation \n, where \n is a number between 1 and 9. Each subexpression brackets and is numbered from left to right.

  • Can I do regular expressions or Boolean logic in the search?

    Can I do regular expressions or Boolean logic in the search? (Windows + F) Suppose I want to search pdf files or text files. I can go * .txt | * .pdf?

    I searched for about an hour for a simple answer to this and this is the closest, I came, but it still not answering the question.

    Can I use expressions simple boolian in instant search and if yes what are.

    I think that AND and WOULDN'T cover 90% of what I want.

    I want to search for emails for things such as [Minutes AND project x]

    Currently, this property returns all the messeges with minutes and all messages with project x.
    Since I have minutes of many projects, and many emails with project x not the minutes that returns are many.   I would use a simple AND to get the intersection.

    If and expression exist, I have found no reference to it.

    According to me, the back had these expressions in the search function.

    Thank you

  • Divide a CSV with a regular Expression

    I'm working on a script that will read the CMYK values from a CSV file and add the nuances of the Swatches palette. I can easily split the CSV entering a comma (.split(",");), but I can't seem to get a regular expression to separate by commas and new lines.

    Here is the code snippet I have right now is not working:

    var fileIn = new File("/Users/brianp/Desktop/AVL-PMS-TEMP.csv");
    fileIn.open();
    var csvIn = fileIn.read();
    fileIn.close();
    
    var regEx = "/,|\\n/";
    csvRecords = csvIn.split(regEx);  
    

    I am barking the wrong tree here?

    I could change the CSV advance to find all line breaks and replace them with commas, but it looks more elegant to adapt my code for the CSV file is delivered.

    Thanks in advance!

    csvRecords = csvIn.split(/[,\n\r]/);

    This is the shortcut. To preset the regex, to leave out the quotation marks:

    var regEx = / | \n/ ;

    Better to add the CR (\r) as well.

    Use the classes of characters [...] If you can instead of alternatives (... |...), they are more effective (allegedly).

    Peter

  • regular expression replacement


    Hi gurus,

    I have a requirement where I have to replace some characters in duplicate and retain the values of this column in order of alphabatic and digital control. Please find below examples of data

    Thanks in advance

    sample data:

    SELECT 'ZZYYXXAAABBBDDDCCC' DOUBLE COL1

    UNION ALL

    SELECT 'DDDCCCAAA' OF THE DOUBLE

    UNION ALL

    SELECT 'SDBBACCCC' OF THE DOUBLE

    UNION ALL

    SELECT "99988866332154" DOUBLE

    UNION ALL

    SELECT "77663322996" DOUBLE

    Expected:

    ABCDXYZ

    ADC

    ABCD

    12345689

    23679

    Hello

    I don't think that regular expressions will help with that.

    Here's a way to do it:

    WITH single_characters AS

    (

    SELECT DISTINCT

    col1

    , SUBSTR (col1, LEVEL 1) AS a_char

    OF sample_data

    CONNECT BY LEVEL<= length="">

    AND PRIOR col1 = col1

    AND PRIOR SYS_GUID () IS NOT NULL

    )

    SELECT LISTAGG (a_char) WITHIN GROUP (ORDER BY a_char) AS expected

    OF single_characters

    GROUP BY col1

    ;

    This assumes that col1 is unique.  If this is not the case, use all that is unique, even something drift of ROWID or ROWNUM, in CONNECT BY and GROUP BY clauses.

    The output I get is exactly what you posted:

    EXPECTED

    --------------------------------------------------------------------------------

    23679

    12345689

    ACD

    ABCD

    ABCDXYZ

    I guess that's what you really want.

    Regular expressions can not re - organize a string so that the characters are in order; you need to split the string into pieces, order parts and then put back together them.  Given that you have to do it just to get the characters in order, it's simple remove duplicates without problem with regular expressions.

  • Regular Expression search and replace question - please help!

    I was wondering if someone could help me I have a lot of paper with notes like this: [1], but I need them to look like in this [1].

    I don't know what to put in the section find [[\d]*] , but I need help with what to put in the field replace to make < sup > numbers]

    In fact, your regular expression is false. What you need is the following:

    (\[\d+\])

    The field replace must contain this:

    $1

  • Number of shaped with preg_replace Regular Expression and PHP

    Hello

    I would like to add a 'dash' after every 3 digits in a given number (10 digits). For example, 9785678941 became 978-567-894-1. How could I achieve this with regular expression using PHP preg_replace?

    Thank you.

    The next solution is based on the example of "The use of backreferences followed literals digital" published on the php.net site.

    In accordance with the $string, $pattern, $replacement nomenclature which is the php.net example use, here´s my modification:

    <>

    $string = '9785678941';

    $pattern = ' / (\\d{3})(\\d{3})(\\d{3})(\\d{1)} /';

    $replacement = ' ${1}-{2}-${3}-${4}';

    echo preg_replace ($pattern, $replacement, $string);

    ?>

  • RESOLVED: Understand regular expressions in search and replace - use it to replace everything between two comments

    I solved this by using the following regular expression in find DW and replace dialogue, it will be all the html on several lines between two specified comments:

    <!-NameofStartingComment--(.| \s)*?--NameofEndingComment-->)




    Hello

    I have structured my site in dreamweaver with each page between the comments section:

    <! - navBarStart - >
    HTML here
    <! - naveBarEnd - >

    I used this method because I wanted to be able to easily use the find and replace dialogue with regular expressions to update pieces of code throughout my site. However, I have real problems finding the correct regular expression to use.

    So in every page I have my <! - startHtmlSection - > <!-endHtmlSection-> between these two tags, I sometimes have different HTML. I want to find the regular expression I could use 'generic' of all the html to a find a replacement between two of my comments specified. Can anyone help? I googled and tried everything what is obvious (after you have selected the regular expressions in the find and Replace dialog) but DW never find the tags in the pages of sites.

    I solved this by using the following regular expression in find DW and replace dialogue, it will be all the html on several lines between two specified comments:

    Hello

    I have structured my site in dreamweaver with each page between the comments section:


    HTML here

    I used this method because I wanted to be able to easily use the find and replace dialogue with regular expressions to update pieces of code throughout my site. However, I have real problems finding the correct regular expression to use.

    So in every page I have my between these two tags, I sometimes have different HTML. I want to find the regular expression I could use 'generic' of all the html to a find a replacement between two of my comments specified. Can anyone help? I googled and tried everything what is obvious (after you have selected the regular expressions in the find and Replace dialog) but DW never find the tags in the pages of sites.

Maybe you are looking for

  • Apple Hardware Test Error: 4BAT/6/40000005: 0x898fb90

    Apple Hardware Test Error: 4BAT/6/40000005: 0x8984fb90 Anyone know what this code hardware error test means or is for?  I just had to replace my trackpad again today because it was not moving.  Now, my computer works fine.  I did a test of material j

  • PC HP Mini 210-2007sl

  • Android not responding to the stylus

    I recently bought a Thinkpad (barely) used running Ice Cream Sandwich and the touch screen works fine but the pen doesn't do anything. I've updated Android settings, and he says the Tablet is up-to-date, but the screen is still on which pens. The sol

  • How the people I share files Skydrive with add and edit files with in folders?

    I sent many people share files in Skydrive, but they claim that they are unable to upload or modify files in the folder, because there is no link or another icon to click on to add files.  How these other users add files on my skydrive?

  • Import contact list

    If I exported my contacts list on my sd card, and then if I do a factory data reset I'll be able to import my contacts from sd card list?