Simple String replacement problem
I want to replace every string of the type:
"^\\d+,[^,]+,,[^,]*,[^,]*$"
to:
"^\\d+,[^,]+,1,[^,]*,[^,]*$"
So that "1234,something,,something,something" becomes "1234,something,1,something,something"
What's the best way to do this?
What are your EXACT requirements?
Do you want to replace all ,, with ,1, ?
Only a ,, that comes after number,something?
Right now, it looks like replaceFirst(",,", ",1,") will work. I doubt that's what you want, but there are probably a whole bunch of different regexes that could turn that input into that output.
Similar Messages
-
Simple string / bytes problem
Hi
I'm basically trying to simulate DES in java from scratch (so no use of existing API's or anything). I've got a basic idea of how DES works (use of a feistel cipher).
So to start off I'm trying to get the bytes of a string with this simple program:
public class Test {
public static void main(String[] args) {
String testString = "hello";
System.out.println(testString.getBytes());
}The output consists of some characters (namely numbers, letters and 1 special character).
But when I change the variable testString to some other String, complie and run I still get the same output even though the String is different. Also why are there 9 characters for a String thats only 5 characters long?
ThanksWhen you use System.out.println( something ) it uses to the toString() method of the object referenced by 'something'. When the something is an array such as an array of bytes the toString() methed returns a pseudo reference to the array which in no way reflects the content of the array.
If you just want to see the content of the array then use Arrays.toString(something) to generate a String representation of the content of the array.
Also, the use of String.getBytes() will convert the String to bytes using your default character encoding. This is rarely what you want since the result may depend on which platform you are working on. In my view, the safest and best approach is to explicitly define the encoding and since it will encode ALL characters one can throw at it I always use utf-8. So, to convert a String to bytes I would usebyte[] bytesOfString = "your string".getBytes("utf-8");and to convert them back to a String I would useString yourString = new String(bytesOfString,"utf-8");One final point, do not assume that an apparently random array of bytes such as one gets from DES encryption can be converted to a String using the above. In general it can't because not all bytes and byte sequences are valid for a particular character encoding. If you absolutely have to have a String representation then you should encode (not encrypt) the bytes using something like Base64 or Hex encoding.
Edited by: sabre150 on Jan 27, 2008 3:04 PM -
Regarding String Replacing ....
Hi Friends,
I have some problem regarding String replacing.
ex.
I have following string ::
=================================================
This is the java manager the main purpose for the jasper is used to create reports.
And the Java language is very extensible.
Is this the proper string functions provided by java isn't it ?
*================================================*
*i want to replace word "is" with "IS". but that word is not connected to word like Th{color:#ff00ff}is{color}*
*differ from simple separate "{color:#ff0000}is{color}" so Word should not be replaced in This only separate "is"*
*must be replaced. ?*
*Any Idea ?*
*{color}*
Edited by: Ghanshyam on Sep 14, 2007 4:27 PMHow about just searching for and replacing
" is "
instead of
"is"
So, using this code from the Java Developers Almanac 1.4 (2002 Addison-Wesley):-
static String replace(String str, String pattern, String replace) {> int s = 0;> int e = 0;> StringBuffer result = new StringBuffer();> > while ((e = str.indexOf(pattern, s)) >= 0) {> result.append(str.substring(s, e));> result.append(replace);> s = e+pattern.length();> result.append(str.substring(s));
return result.toString();
}Your would use:-
String newStr = replace(originalStr, " is "," IS ");
Edited by: mad_scientist on Sep 14, 2007 4:16 AM (Sorry about all the edits, just getting used to the formatting on here)
Edited by: mad_scientist on Sep 14, 2007 4:22 AM
Edited by: mad_scientist on Sep 14, 2007 4:24 AM -
PLD String Replace Function?
Hello Experts,
I would like to use the Sum in Words option in the field properties Format tab. However, I would like to have this in a language for which there is no LRF.
I have read all the various Sum in Words/Amount in Words threads I can find but I have no special locale based requirements (lacs etc). The default answer seems to be to prepare a FMS query and attach it to a UDF, then pull the variable into PLD.
Rather than rewriting the functionality, it would be far easier for me to use a series of Replace functions (the structure of Turkish for amount in words is exactly the same as English and translating each word (One, Two... Ten, Twenty... etc) with a Replace would solve my problem.
Looking at the documentation for PLD, I can see no prebuilt Replace function - is there any way this can be achieved?
If not, is there any Sum in Words function in B1 queries so I can at least do the replace there rather than writing an entire convert to words query?
Thank you.Wasn't able to force a string replace - had to hex edit the LRF instead. Quick and dirty, but it works.
-
I am looking to add my date to the beginning of my articles.
Previously, I
was using the string replace function to look for the
begininng of my
paragraph and then place the date in front of that. Bad Jon!
I forgot that
any article with multiple paragraphs would then have multiple
dates.
$artContent = str_replace('<p>', '<p>
'.$artDateEdited.' - ',
$artContent);
So, what I am looking for is how do I do a string replaceon
just the first
paragraph?
TIA,
Jon Parkhurst
PriivaWeb
http://priiva.net.Are you sure there are <p> tags in the data? The only
way that could get
there would be if someone explicitly put them there.
Murray --- ICQ 71997575
Adobe Community Expert
(If you *MUST* email me, don't LAUGH when you do so!)
==================
http://www.dreamweavermx-templates.com
- Template Triage!
http://www.projectseven.com/go
- DW FAQs, Tutorials & Resources
http://www.dwfaq.com - DW FAQs,
Tutorials & Resources
http://www.macromedia.com/support/search/
- Macromedia (MM) Technotes
==================
"crash" <[email protected]> wrote in message
news:[email protected]...
> LOL, just now getting back to this.
>
> I've got myself in a little bit of a bind, b/c I didn't
foresee my data
> having more than one paragraph.
>
> This is what I'm thinking of doing. If there's a better
way, can somebody
> pipe in?
>
> Take data from database.
> Search for <p> tags, and capture all text within
the first instances
> Limit text to X number of chars
> Replace <p> tags, and my ...Full Story links
>
> Looking now through all of the string functions and
preg_replace right
> now.
>
> Basically, I"m having some problems finding a function
that will replace
> $var on the Xth instance.
>
> Will post tomorrow with my results.
> "Joe Makowiec" <[email protected]> wrote in
message
>
news:[email protected]...
>> On 26 Aug 2006 in macromedia.dreamweaver.appdev,
crash wrote:
>>
>>> I am looking to add my date to the beginning of
my articles.
>>> Previously, I was using the string replace
function to look for the
>>> begininng of my paragraph and then place the
date in front of that.
>>> Bad Jon! I forgot that any article with multiple
paragraphs would
>>> then have multiple dates.
>>>
>>> $artContent = str_replace('<p>',
'<p>
>>> '.$artDateEdited.' - ',
>>> $artContent);
>>>
>>> So, what I am looking for is how do I do a
string replaceon just the
>>> first paragraph?
>>
>>
http://www.php.net/manual/en/function.preg-replace.php
>>
>> In particular, the optional 4th parameter to
preg_replace() is a limit;
>> you can set this to 1.
>>
>> --
>> Joe Makowiec
>>
http://makowiec.net/
>> Email:
http://makowiec.net/email.php
>
> -
Hi, anyone knows how to replace a particular string in a file with another string?
For example, in the file hello.tmp,
I want to replace the word :Smile: with the string
<img src="smile.gif">
I tried to use String.replace() but this is only for characters.
Anyone knows how to replace Strings??
Hope to hear from anyone really soon. Very urgent!!!There is no direct method to replace a string in file. The code goes like this:
import java.io.*;
import java.util.*;
public class StringReplacer
private String replaceString;
private String replacerString;
private String filePath;
public StringReplacer(String replaceString, String replacerString, String filePath)
this.replaceString = replaceString;
this.replacerString = replacerString;
this.filePath = filePath;
try
replace();
catch (Exception ex)
private void replace() throws Exception
File file = new File(filePath);
if(!file.exists())
System.out.println("File doesnot exists");
return;
DataInputStream dis = new DataInputStream(new FileInputStream(file));
StringBuffer fileData = new StringBuffer("");
String strTemp = "";
while((strTemp = dis.readLine()) != null)
fileData.append(strTemp);
dis.close();
StringTokenizer stTemp = new StringTokenizer(fileData.toString(), replaceString);
int tokens = stTemp.countTokens();
if(tokens <= 1)
//No matches found for string to be replaces.
System.out.println("No matches found for string to be replaced");
return;
fileData = new StringBuffer("");
while(stTemp.hasMoreTokens())
fileData.append(stTemp.nextToken() + replacerString);
if(file.exists())
file.delete();
file = new File(filePath);
if(!file.exists())
file.createNewFile();
DataOutputStream dos = new DataOutputStream(new FileOutputStream(file));
dos.writeChars(fileData.toString());
dos.flush();
dos.close();
public static void main(String [] args)
//create an object of StringReplacer here.
} -
What is the unicode for '' (need it to do String.replace)
i'm trying to use String.replace(char a, char b)
it won't compile if i do x.replace('_', '')
help!
thanks!Are you trying to remove all occurences of the underscore character from the string? If so create a StringBuffer from the string, loop through it and remove the characters as you find them.
If you're using JDK1.4 you should eb able to use the new String method:
public String replaceAll(String regex,
String replacement) -
String has a replace method for chars, something the likes of
String replace(char a, char b)
I tend to think it would be nice to ammend String further with
String replace(String a, String b)
proponents/opponents? It seems like a nice thing to have.This type of method is available in the regex package
(available in JDK 1.4). I am not expert or regular
expressions but it will allow you to do this and much
much more.I guess it will. Still it would be nice to have the ability directly inside String without explicitly having to instantiate a Pattern. I know there is a split method in String in JSDK 1.4 which takes a String specification of a regexp and internally compiles it. I guess my point is convenience but I should know the new APIs fully before writing more. -
SIMPLE Database Design Problem !
Mapping is a big problem for many complex applications.
So what happens if we put all the tables into one table called ENTITY?
I have more than 300 attributeTypes.And there will be lots of null values in the records of that single table as every entityType uses the same table.
Other than wasting space if I put a clustered index on my entityType coloumn in that table.What kind of performance penalties to I get?
Definition of the table
ENTITY
EntityID > uniqueidentifier
EntityType > Tells the entityTypeName
Name >
LastName >
CompanyName > 300 attributeTypes
OppurtunityPeriod >
PS:There is also another table called RELATION that points the relations between entities.>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
check the coloumn with WHERE _entityType='PERSON'
as there is is clustered index on entityType...there
is NO performance decrease.
there is also a clustered index on RELATION table on
relationType
when we say WHERE _entityType ='PERSON' or
WHERE relationType='CONTACTMECHANISM'.
it scans the clustered index first.it acts like a
table as it is physically ordered.I was thinking in terms of using several conditions in the same select, such as
WHERE _entityType ='PERSON'
AND LastName LIKE 'A%' In your case you have to use at least two indices, and since your clustered index comes first ...
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
Have you ever thought of using constraints in your
modell? How would you realize those?
...in fact we did.We have arranged the generic object
model in an object database.The knowledge information
is held in the object database.So your relational database is used only as a "simple" storage, everything has go through your object database.
But the data schema is held in the RDBMS with code
generation that creates a schema to hold data.If you think that this approach makes sense, why not.
But in able to have a efficent mapping and a good
performance we have thought about building only one
table.The problem is we know we are losing some space
but the thing is harddisk is much cheaper than RAM
and CPU.So our trade off concerated on the storage
cost.But I still wonder if there is a point that I
have missed in terms performance?Just test your approach by using sufficiently data - only you know how many records you have to store in your modell.
PS: it is not wise effective using generic object
models also in object databases as CPU cost is a lot
when u are holding the data.I don't know if I'd have taken your approach - using two database systems to hold data and business logic.
PS2: RDBMS is a value based system where object
databases are identity based.we are trying to be in
the gray area of both worlds.Like I wrote: if your approach works and scales to the required size, why not? I would assume that you did a load test with your approach.
What I would question though is that your discussing a "SIMPLE Database Design" problem. I don't see anything simple in your approach when it comes to implementation.
C. -
Hot Code Replace Problem...
Hi Everyone,
I am using WebLogic 10 with Eclipse. Whenever i start the server in debug mode and make two three changes in the code... WHAM.... I get the Hot Code Replace error. Is there any way so that the server can keep up with the code changes. Publishing the app again takes approx 40 mins!Almost always this is a limitation of the JRE/JDK you are using and really
nothing to do with Eclipse.
Try a different VM and see if you have better luck
HTH
Darins
"bobz" <[email protected]> wrote in message
news:75257188f239cb376da31e19b214d2d9$[email protected]..
> hi buddies,
>
> i am getting "hot code replace" problem whenever i change my
> java code and build the project and access the website without re-starting
> the server. so, finally i endup with re-starting the server whenever i
> change the code. this consumes a lot of time. is there any possibility to
> work without re-starting the server even after the code change.
>
> regards,
> satish. -
Batch Rename with String Replacement?
Is there a convenient way to batch rename files after they've been imported? The template mechanism seems to work well for creating new names, but not for renaming. For example, I originally imported and renamed my images to look like this:
Zemke_YYMMDD_NNN-O1.CR2
I decided I would rather have DRZ (My initials) instead of "Zemke" as the prefix. I was able to get it done with some contortions:
Metadata/XMP/Export
Remove all files from Lightroom
Rename all the external files to start with DRZ
Reimport all the files
Delete the XMP files
I think what is needed is for Rename to include string replacement (and string deletion) functionality. Is it already there?
DanStefano, nonadjacent selection can be done by holding down Control and manually selecting the additional image. If you want to select a lot of separate ranges, the way to do it is to add each range to the quick collection (press the B key), then go to the quick collection and select all. It's exactly for those kinds of scenarios that quick collection exists. Once you get used to doing it, it's easier working with a "sticky" selection like the quick collection rather than the contortions required to extend standard selections.
To slightly add to Rory's comments: two-digit year plus spelled out month names and day of the week names are also available. Combine into any order you want, with any additional supporting text anywhere you want it, and you've got huge flexibility there. And no matter how complex it is, you can save it as a preset so it's readily accessible later. -
Whole word string replacement in LV7.1
Is there anyway to do whole word string replacement in 7.1?
For example, if I have the string "x21+sin(x2)" and I want to replace "x2" with "b2" without accidently changing "x21" into "b21". I saw that 7.1 does not support the word boundary "\b" in regex.The easy solution is to search for "(x2)" and replace with "(b2)" but if you're not sure that x2 will always be in parenthesis, then another solution would be to get the next character after "x2" is found, and if it is not a number (0-9) then you have found x2, if it is a number then you have found x21, or x22, etc.
Unofficial Forum Rules and Guidelines - Hooovahh - LabVIEW Overlord
If 10 out of 10 experts in any field say something is bad, you should probably take their opinion seriously. -
Help needed with String replace!
Hi,
I would like to replace all '\' characters with '/' in my string, ie:
mystring.replace('\','/');
Howeve, it complain about the '/' (Unclosed character literal).
Any suggestions?
ThanksShow us the line, you are probably using your regex incorrectly.
In order to get the '\' you have to escape it (the '\' char. is used as the escape character so that you can look for things like " ' and new lines by useing \" \' and \n. Otherwise, how would java know you are looking for quotes and not closing your string?) As a consequence, your regex should look like '\\" for the \.
The replace statement should probably be:
string.replace("\\", "/"); -
Hi All,
How do i replace the text with text that has $.
String s = "Sample text XYZ";
s.replaceAll(XYZ, "$ABC");
Error: Illegal Group Reference.
"\\$ABC" works.
In my case "$ABC" is dynamic text. Position of $ can be any where in in the text, need not be first char.
TIA,
Kishore.I think you'd need to find the $ in your search string first and put the escape character in their before attempting to do the replace all. In case you're unaware, RegEx uses $ as a special character, so that's why it must be replaced..
String a = "This is XYZ";
String replace = "$ABC";
StringBuffer sb = new StringBuffer(replace);
sb.insert(sb.indexOf("$"), "\\ ");
a = a.replaceAll("XYZ", sb.toString());
System.out.println(a);(Note: The extra space after the \\ in the insert was just so this forum would display everything properly). -
I am completely new to JSP and I have a string which contains spaces but I need to replace all of the spaces with underscores (_)? Any help anyone can give me would be much appreciated!
I think you are new to JAVA too..
ok fine
use this method
String replace(char oldChar, char newChar)
Example
String sSpace = "A ";
sSpace = sSpace.replace(' ','_');
Maybe you are looking for
-
Unable to read the dictionary of the application or extension...
I had previously installed the HP Color LaserJet 3600 driver from the install CD under Tiger. I recently did an archive and install of Leopard, then ran all the system updates. (Currently at 10.5.5) When I insert the 3600 CD, though, I get a bunch of
-
I get this message a lot on starting up Firefox and it also comes at times while I am in the midst of an internet session. Firefox starts very, very slowly just before this message surfaces (10 to 15 minutes before I get the opening page). Is this a
-
Pretty much what's up there. If there's a Flash open, and I scroll down/open a window/open a new tab such that it is not visible anymore, it will not be animated. It freezes until any portion of it is visible again. This wasn't happening before a fla
-
Where is my total saying how many songs are in my library?
i have just downloaded the new itunes last night (3-31-13) and started to use it today (4-1-13) and i don't have the total at the bottom anymore and would like to see it because i like to see the total of my library... ....on a side note the library
-
How to un hide Industry type in material Master - mm01
Dear All Experts Kindly help to unhide Industry Type field in Material Master . Regards Alok