Replace multiple characters to single character
Hi friends,
I would like to replace multiple characters in a string to a single character. The character may be a pipe (|) or a tilde (~).
For example "|||asdf||123|xyz||" should be changed to "asdf|123|xyz".
I use Oracle 11g2.
Thanks in advance!
Without regexp
with testdata as (
select '||asdf||||123|xyz|' str from dual union all
select 'asdf|123|||||||xyz||||' from dual union all
select '||||||asdf|||123||||||xyz||||||' from dual
select
trim (both '|' from
replace (
replace (
replace (str,'|','|'||chr(0))
,chr(0)||'|'
,chr(0)
) str
from testdata
STR
"asdf|123|xyz"
"asdf|123|xyz"
"asdf|123|xyz"
with regexp
with testdata as (
select '||asdf||||123|xyz|' str from dual union all
select 'asdf|123|||||||xyz||||' from dual union all
select '||||||asdf|||123||||||xyz||||||' from dual
select
trim (both '|' from
regexp_replace (
str
,'\|+'
,'|'
) str
from testdata
STR
"asdf|123|xyz"
"asdf|123|xyz"
"asdf|123|xyz"
Message was edited by: chris227 regexp added
Similar Messages
-
Replacing multiple spaces with another character
i need to replace multiple spaces with another char in a string
my code is
Dim text as String="a bit much a little much" Dim arr As String() = text.Split({" "c}, StringSplitOptions.RemoveEmptyEntries)Dim newtext As String = String.Join(" ", arr) MsgBox(newtext)
it returns to "a bit much a little much "
But i want to replace only long spaces with "=" as "a bit much =a little much"
How can i do that?This version uses two spaces to split the string and then trims any additional single spaces. It returns "a bit much = a little much".
Dim text As String = "a bit much a little much "
Dim parts() As String = text.Split({" "}, StringSplitOptions.RemoveEmptyEntries)
Dim newText As String
If parts.Length = 2 Then
newText = parts(0).Trim & " = " & parts(1).Trim
End If -
Replace multiple characters in a string
Hi, I have some string fields that contain special characters such as ô and û and I want to replace them with ō and ū, and their upper case equivalents as well. How do I write a single formula that would find any of these characters in a string and replace them?
Thanks,
Willreplace(replace(replace(replace(x,'ô','ō'),'û','ū'),'Ô','Ö'),'Û','Ü');
where x is the string field. I suggest using the unicodes rather than the actual character. I do not think that I have the correct uppercase characters. Please ensure that they are correct in your formula. -
Regex replacing multiple characters in string.
I have been working through the Java regex tutorial and tried to modify one of the programs for my own use. Basically, I want to take a string and convert the chatracters A to T, T to A, C to G and G-C.
I produced the rather crude program below, but of course it doesn't work. A could be converted to T and back again before the program terminates.
I know that the code to do this correctly is probably quite complex, so could anyone point me in the direction of a tutorial which will help me to do this?
This aside, I take it that if I am looking for multiple matches of characters which won't give the problem already indicated above, my code is too bloated anyway. Say, for example, instead of wanting to replace A to T, T to A, C to G and G-C, I wanted to replace dog-cat, horse-donkey - lion, tiger , cat-mouse. My code will work for this, but I am sure that it could be compressed a lot. Surely I would not need all the lines of code to do this?
Thanks for any help,
Tim
import java.util.regex.Pattern;
import java.util.regex.Matcher;
import java.io.*; // needed for BufferedReader, InputStreamReader, etc.
/** A Java program that demonstrates console based input and output. */
class dna {
// Create a single shared BufferedReader for keyboard input
private static BufferedReader stdin =
new BufferedReader( new InputStreamReader( System.in ) );
// Program execution starts here
public static void main ( String [] args ) throws IOException
// Prompt the user
System.out.print( "Type your DNA sequence: " );
// Read a line of text from the user.
String DNA = stdin.readLine();
DNA = DNA.toUpperCase();
String DNA2 = DNA;
//calculate reverse complement
Pattern A = Pattern.compile("A");
Pattern T = Pattern.compile("T");
Pattern C = Pattern.compile("C");
Pattern G = Pattern.compile("G");
Matcher AA = A.matcher(DNA);
DNA = AA.replaceAll("T");
Matcher TT = T.matcher(DNA);
DNA = TT.replaceAll("A");
Matcher CC = C.matcher(DNA);
DNA = CC.replaceAll("G");
Matcher GG = G.matcher(DNA);
DNA = GG.replaceAll("C");
// Display the input back to the user.
System.out.println( "DNA input : " + DNA2);
System.out.println ("Complementary sequence: " + DNA);
}TimM wrote:
Thanks a lot!!! Can't believe you managed all that with so few lines of code.You're welcome.
Must be great to know what you are doing :-)
Thanks again,
TimAfter being a bit more familiarised with the methods of String, you'll be able to do this in no time, I'm sure! -
Replacing multiple nodes with single node in OSB
Hi,
I have a case where I will get a xml like below:
<LINEITEMS>
<LINE_ITEM>
<LINE_ITEMNO>1</LINE_ITEMNO>
<STATUS_CODE>0</STATUS_CODE>
<MESSAGE>MESSAGE1</MESSAGE>
</LINE_ITEM>
<LINE_ITEM>
<LINE_ITEMNO>3</LINE_ITEMNO>
<STATUS_CODE>0</STATUS_CODE>
<MESSAGE>MESSAGE3</MESSAGE>
</LINE_ITEM>
<LINE_ITEM>
<LINE_ITEMNO>1</LINE_ITEMNO>
<STATUS_CODE>0</STATUS_CODE>
<MESSAGE>MESSAGE2</MESSAGE>
</LINE_ITEM>
<LINE_ITEM>
<LINE_ITEMNO>3</LINE_ITEMNO>
<STATUS_CODE>0</STATUS_CODE>
<MESSAGE>MESSAGE4</MESSAGE>
</LINE_ITEM>
<LINE_ITEM>
<LINE_ITEMNO>2</LINE_ITEMNO>
<STATUS_CODE>1</STATUS_CODE>
<MESSAGE>MESSAGE5</MESSAGE>
</LINE_ITEM>
</LINEITEMS>
I need to make this as below:
<LINEITEMS>
<LINE_ITEM>
<LINE_ITEMNO>1</LINE_ITEMNO>
<STATUS_CODE>0</STATUS_CODE>
<MESSAGE>MESSAGE1;MESSAGE2</MESSAGE>
</LINE_ITEM>
<LINE_ITEM>
<LINE_ITEMNO>3</LINE_ITEMNO>
<STATUS_CODE>0</STATUS_CODE>
<MESSAGE>MESSAGE3;MESSAGE4</MESSAGE>
</LINE_ITEM>
<LINE_ITEM>
<LINE_ITEMNO>2</LINE_ITEMNO>
<STATUS_CODE>1</STATUS_CODE>
<MESSAGE>MESSAGE5</MESSAGE>
</LINE_ITEM>
</LINEITEMS>
I need to have only one entry for each line Item.
Please let me know on the approach on how to do it in OSB.
ThanksYou can try this :-
let $out:= <LINEITEMS>{ for $x in 1 to count($input/LINE_ITEM)
let $LineItme := $input/LINE_ITEM[$x]/LINE_ITEMNO
let $match:= for $y in $x to count($input/LINE_ITEM)
where $input/LINE_ITEM[$y]/LINE_ITEMNO= $LineItme
return $input/LINE_ITEM[$y]/MESSAGE
return
<LINE_ITEM>
<LINE_ITEMNO>{$LineItme/text()}</LINE_ITEMNO>
<STATUS_CODE>{data($input/LINE_ITEM[$x]/STATUS_CODE)}</STATUS_CODE>
<MESSAGE>{fn:data($match)}</MESSAGE>
</LINE_ITEM>
}</LINEITEMS>
return $out
You many then have to remove duplicates with line items. use fn:distinct() and then loop through. -
Replace multiple space characters with a single space
Hi,
Oracle 11g R2.
Looking for a way to replace multiple space characters in a row with a single space. For example, the text "abc abc" should look like "abc abc". I tried toying with replace, but it only works for the case of 2 spaces. Need a solution for the cases where there could be 2 or more spaces.
select replace(column1, chr(32)||chr(32), chr(32)) from tableaHi,
If you had to do this without regular expressions, you could use:
SELECT REPLACE ( REPLACE ( REPLACE (str, ' ', '~ ')
, ' ~'
, '~ '
) AS new_str
FROM table_x;assuming there is some sub-string (I used '~' above) that never occurs right next to a space.
However, unless you're uisng Oracle 9 (or earlier, which you're not doing) you don't have to do this without regular expressions. As you can see, the way Solomon showed is much simpler. -
Replacing multiple spaces with a single space
Hi friends,
I have a string. It can have zero/one/multiple spaces. I want to make the multiple spaces to single space.
Here are the cases:
1. ' a b c d efg h' should be changed to 'a b c d e f g h'
2. ' a b c d e f g h ' should be changed to 'a b c d e f g h'
3. 'a b c d e f g h' should not be changed
4. 'abcdefgh' should not be changed
Both REPLACE and TRANSLATE do not help. I don't want to go for LOOP logic. Please help me to get it in SQL query.
Thanks in advance!Hi,
964559 wrote:
Hi friends,
I have a string. It can have zero/one/multiple spaces. I want to make the multiple spaces to single space.
Here are the cases:
1. ' a b c d efg h' should be changed to 'a b c d e f g h'One solution is to post your string on this site, and then copy it back again. (See below for a more serious solution .)
This site is doing exactly what you want the function to do: it replaces multiple consecutive spaces with a single space. As a result, it's hard to see what you mean.
To preserve spacing on this site, type these 6 characters
\(small letters only, inside curly brackets) before and after each section where you want spacing preserved.
2. ' a b c d e f g h ' should be changed to 'a b c d e f g h'
3. 'a b c d e f g h' should not be changed
4. 'abcdefgh' should not be changed
Both REPLACE and TRANSLATE do not help. I don't want to go for LOOP logic. Please help me to get it in SQL query.
Thanks in advance!Regular expressions make this easy:SELECT TRIM ( REGEXP_REPLACE ( str
, ' +'
) AS compressed_str
FROM table_x;
You can use nested REPLACE calls to get the same results, but it's messy.
Edited by: Frank Kulash on Feb 5, 2013 10:18 AM
Added TRIM -
Replace single character and leave the ones in expressions
Hi,
I'm looking for nice and elegant solution to my problem. Let's say I have a string "e+exp(e)". I want to replace all "e" with "1", excepts the one in "exp". I was trying to use regular expressions but I've failed after an hour... The solution should be able to solve things like replace "a" in "rand(a)" without changing rand. Basically I need to replace single character without replacing the ones in words/expressions.
Any ideas?
Certified LabVIEW Developer, Certified TestStand Developer
Solved!
Go to Solution.No time for elegance right now, just brute force. I would pad the string with spaces on each end to remove end cases and then search for the character surrounded by non-word characters.
Message Edited by Darin.K on 04-14-2010 04:06 PM
Attachments:
VariableParsingRegex.png 18 KB -
How to replace multiple occurences of space in a string to a single space?
How to replace multiple occurences of space in a string to a single space?
Hi,
try this code.
data : string1(50) type c,
flag(1) type c,
dummy(50) type c,
i type i,
len type i.
string1 = 'HI READ THIS'.
len = strlen( string1 ).
do len times.
if string1+i(1) = ' '.
flag = 'X'.
else.
if flag = 'X'.
concatenate dummy string1+i(1) into dummy separated by space.
clear flag.
else.
concatenate dummy string1+i(1) into dummy.
endif.
endif.
i = i + 1.
enddo.
write : / string1.
write : / dummy. -
How do I find & replace a single character?
Am using Adobe Acrobat Pro....
The find/replace box pull down has "Whole Words Only" checked and I cannot uncheck it. How can I find and replace a single character throughout a document?That's just how it works. You can "find" anything, but you can only "replace" whole words. As soon as you click to open the "replace" section of the dialog the search settings are locked into their basic states, even if you don't enter a replacement string (but that part's a bug).
The simple answer is that you cannot replace all instances of one character using Acrobat's inbuilt tools. You're expected to edit the document outside of PDF and recreate it. -
ANN: Tutorial on Replacing Multiple or Single Templates in DW on Child Pages
FYI: this article is available for free reprints, as long as
guidelines are
followed:
http://www.petersonsales.net/tutorial1.php
Replacing Old Website Templates in Dreamweaver
One of the most difficult tasks for a web designer is
updating legacy
websites. Legacy websites are websites which have existed for
many years.
Often these sites have outdated code or are badly in need of
an upgrade. For
years, Dreamweaver has used templates to allow an entire site
to be updated
at once simply by making changes to a master page. But what
do you do when
the template itself was developed with outdated code and the
site needs a
complete design overhaul? How do you fix it without having to
cut and paste
the content from every existing page to a newly designed one?
Part 1 - Overwriting a Single Template
Part 2 - Replacing Multiple Templates with One
~~~~~~~~~~~~
Jefferis Peterson, Pres.
Web Design and Marketing
http://www.PetersonSales.comWhat Ken has said also applied to
<img src="/assets/images/Lights/lights_select.jpg" width="400" height="400" alt="Lighting Thumbnails" />
which look for the JPG in http://davidcoshdesign.com/assets/images/Lights/lights_select.jpg rather than http://davidcoshdesign.com/nea/assets/images/Lights/lights_select.jpg
Gramps -
Replacing multiple new line characters
how to replace multiple new line characters in a line.
Suppose for example my line contains Example1#####Example2##Example3###Example4,
then the output should be Example1#Example2#Example3#Example4.Hi Sid,
You Can try this code
DATA: ld_string TYPE string,
ld_string1 TYPE string,
ld_string2 TYPE string,
ld_string3 TYPE string.
ld_string = 'Example1#####Example2##Example3###Example4'.
REPLACE ALL OCCURRENCES OF '######' in ld_string with '#'.
REPLACE ALL OCCURRENCES OF '####' in ld_string with '#'.
REPLACE ALL OCCURRENCES OF '###' in ld_string with '#'.
REPLACE ALL OCCURRENCES OF '##' in ld_string with '#'.
write:/1 ld_string.
Regards
Pavan -
Replace multiple whitespaces in a string with single one
Hi,
I'm using Oracle 9.2.0 and I have a query on string manipulation.
Can I replace multiple whitespaces within a string with single one. I tried with regexp_replace function but it is not supporting Oracle 9i.
Can someone help me out?
Regards,
PramodJust playing around:
WITH
Strings
AS
SELECT 'Can I replace multiple whitespaces within a string with single one' Text FROM Dual
SELECT
REPLACE
REPLACE
XMLAGG
XMLElement
"A",
SubStr(Text, Level, 1)
'</A>'
'<A>'
) Text
FROM
Strings
WHERE
NOT
Level > 1
AND SubStr(Text, Level, 1) = ' '
AND SubStr(Text, Level - 1, 1) = ' '
CONNECT BY
Level <= LENGTH(Text); -
Multiple Entries in Single Field (seperate data...?)
I have some mess of data and curious on if this can resolved simple from a query.
What I have, is multiple entries in a single cell. All the data is separated by pipes(|).
So, I have data similar to this in a table.(all made up of course :))..
Soda, coke|pepsi|dew|dtcoke,
Computer, dell|hp|gateway,
What I'm looking to do, is to turn these values into separate columns. So the final output would look
soda, coke
soda, pepsi
soda, dew
soda, dtcoke
computer, dell
computer, hp
computer, gateway
Any suggestions. And, sorry for the hefty examples. Figured more the better....
Edited by: Topher34 on Sep 5, 2008 9:20 AMHi,
Topher34 wrote:
With that, ...With what? There are a lot of queries in the thread to which C linked, not to mention links from there. If you need an explanation of any of them, post a copy.
I think this is what you want:
WITH cntr AS
SELECT LEVEL AS n
FROM dual
CONNECT BY LEVEL <= 1 +
( SELECT MAX (REGEXP_COUNT (b_list, '[|]+'))
FROM table_x
SELECT a
, b_list
, REGEXP_SUBSTR ( b_list
, '[^|]+'
, 1
, n
) AS b
FROM table_x
JOIN cntr
ON n <= 1 + REGEXP_COUNT (b_list, '[|]+')
ORDER BY a
, n;In Oracle 11, this produces the following output from the sample data you posted (that is, the first two distinct columns):
A B_LIST B
Computer dell|hp|gateway dell
Computer dell|hp|gateway hp
Computer dell|hp|gateway gateway
Soda coke|pepsi|dew|dtcoke coke
Soda coke|pepsi|dew|dtcoke pepsi
Soda coke|pepsi|dew|dtcoke dew
Soda coke|pepsi|dew|dtcoke dtcokeREGEXP_COUNT is new in Oracle 11, but here's how to adapt it for Oracle 10:
The solution above finds the number of entries in b_list (entries delimited by one or more consecutive | characters) like this:
1 + REGEXP_COUNT (b_list, '[|]+')In Oracle 10, you can get the number of entries delimited by a single | character like this:
1 + LENGTH (b_list) - LENGTH (REPLACE (b_list, '|', NULL))The results will be the same if b_list does not contain consecutive | characters. Otherwise, you may want to test for NULL values in column b.
Edited by: Frank Kulash on Sep 30, 2008 3:32 PM
Originally posted with error.
In the Oracle 10 work-around, when this expression
1 + LENGTH (b_list) - LENGTH (REPLACE (b_list, '|', NULL))is used in sub-query cntr, it is to find the worst case across the whole table, so it should be the argument of MAX. Say
MAX (1 + LENGTH (b_list) - LENGTH (REPLACE (b_list, '|', NULL)))or
1 + MAX (LENGTH (b_list) - LENGTH (REPLACE (b_list, '|', NULL)))In the main query, it applies just to the single value in the current row, so you should not use MAX there. -
Typing in s single character in an email address results in many...
I have this strange problem when I attempt to type in an email address in an email, or I try to add an email address to an address book contact. As I type a single character I will get multiple characters typed for that single keystroke, (i.e., if I type "k" I will get "klstp". and if I attempt to backspace, f4, or any key, it will continually apply the same "klstp" for that instance.) This behavior will go away and reappear at no known pattern or time. One thing that is additionally wierd is the characters that will show up are always characters that are contained in that persons email address or somewhere in their address book listing.
Some of the troubleshooting I have already attempted to do is use another keyboard, run Onyx utility to repair the system, but nothing seems to work.Thanks for your suggestion, but I understand the previous recipients function that you are speaking of. This is not that. This is random character sets that show with a single character being typed, but it is not any combination that exists from previous recipients. As an example if I type the letter S, I will get the letters SDQT, if I then hit backspace to remove it it will again type SDQT, if I then use an arrow key to try and move the position of the curser, it will again type SDQT. The only key I have found that will not do this is the ESC key. The important part here is that the combination SDQT is not part of an email address anywhere in my address book or previous recipients, and this combination of characters is only an example, the actual characters appear to be random.
Maybe you are looking for
-
Cyan overprint in white border around pix
Using Epson 1900 and CS5--- Using Colormunkie created paper profile for Epson paper - using any paper surface and problem exists.. It is printing a light cyan overprint on the white borders around the pictures. There is no cyan overprint in the pictu
-
Installation Without Bundlet JRE
Hi, i want to install JSC but it sends me that it can not start graphical mode. i already know what the problem is: i'm installing JSC in my linux machine, but it java has a bug about xde, and xinerama, i already found a workaround for this, java and
-
Is there a way to use a G5 imac as a monitor? The processor is dead but the monitor still works.
-
Uninstall/install difficulties!
I'm having difficulties with iTunes I was updating it, but in the middle of that I had to switch the computer off. When I got back on it again I wanted to do the full installation. That obviously didn't work... I tried to uninstall and re-install it
-
http://forums.palm.com/t5/webOS-Software/The-document-viewer-issue/td-p/316620 This is another roadblock in a fix for above post. While trying to get a usable method to zoom in and read some data, I saved it as a Jpeg. This worked good and I can zoom