473,378 Members | 1,549 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Regular expression question!

3
Hi everyone,
I would like to ask something about regular expressions in java.

I have made a code lika that

Expand|Select|Wrap|Line Numbers
  1. cleanString = cleanString.replaceAll(";", " ; ");
  2. cleanString = cleanString.replaceAll(":", " : ");
  3. cleanString = cleanString.replaceAll(",", " , ");
  4. cleanString = cleanString.replaceAll("`", " ` ");
  5. cleanString = cleanString.replaceAll("!", " ! ");
  6.  
Can I do it this with only one regular expression, because it seems very bad code.

I know that the first parameter could be
replaceAll("[;:,`!]", ) but the second parameter?



I would like the same for the expression

Expand|Select|Wrap|Line Numbers
  1. cleanString = cleanString.replaceAll("That's", " That is ");
  2. cleanString = cleanString.replaceAll("that's", " that is ");
  3.  
Thanks a lot
Jun 10 '07 #1
6 1455
JosAH
11,448 Expert 8TB
Hi everyone,
I would like to ask something about regular expressions in java.

I have made a code lika that

Expand|Select|Wrap|Line Numbers
  1. cleanString = cleanString.replaceAll(";", " ; ");
  2. cleanString = cleanString.replaceAll(":", " : ");
  3. cleanString = cleanString.replaceAll(",", " , ");
  4. cleanString = cleanString.replaceAll("`", " ` ");
  5. cleanString = cleanString.replaceAll("!", " ! ");
  6.  
Can I do it this with only one regular expression, because it seems very bad code.

I know that the first parameter could be
replaceAll("[;:,`!]", ) but the second parameter?



I would like the same for the expression

Expand|Select|Wrap|Line Numbers
  1. cleanString = cleanString.replaceAll("That's", " That is ");
  2. cleanString = cleanString.replaceAll("that's", " that is ");
  3.  
Thanks a lot
Read all about it in the Matcher class; for your example it would look like this:

Expand|Select|Wrap|Line Numbers
  1. cleanString= cleanString.replaceAll("[;:,`!]", " $0 ");
  2.  
Note the spaces around the $0. The $ group designator refer to parts of the
previous regular expression or to the entire thing (denoted by $0).

kind regards,

Jos
Jun 10 '07 #2
pro
3
Thank you very much!

I would like also to ask what about brackets

How can I reaplace with something else . . ?
Jun 10 '07 #3
JosAH
11,448 Expert 8TB
Thank you very much!

I would like also to ask what about brackets

How can I reaplace with something else . . ?
I'm sorry, I don't understand your question; brackets and parentheses can be
escaped by backslashes but I don't know if that's what you're looking for.

kind regards,

Jos
Jun 10 '07 #4
pro
3
I'm sorry, I don't understand your question; brackets and parentheses can be
escaped by backslashes but I don't know if that's what you're looking for.

kind regards,

Jos
I found it, is
cleanString = cleanString.replaceAll("[\\)\\(\\[\\]]", " $0 ");

But I didn't understand how can I replace strings that I don't care if the characters are little or big.
I mean That's,that's,ThAT's to be all the same.

replaceAll("...",)????
Jun 10 '07 #5
JosAH
11,448 Expert 8TB
I found it, is
cleanString = cleanString.replaceAll("[\\)\\(\\[\\]]", " $0 ");

But I didn't understand how can I replace strings that I don't care if the characters are little or big.
I mean That's,that's,ThAT's to be all the same.

replaceAll("...",)????
Read about all the posibilities in the Pattern API documentation; maybe a
simple \w will do for your purpose or a POSIX character class ...

kind regards,

Jos
Jun 10 '07 #6
prometheuzz
197 Expert 100+
I would like the same for the expression

Expand|Select|Wrap|Line Numbers
  1. cleanString = cleanString.replaceAll("That's", " That is ");
  2. cleanString = cleanString.replaceAll("that's", " that is ");
  3.  
Try something like this:
Expand|Select|Wrap|Line Numbers
  1. String s1 = "ThAt'S it!";
  2. String s2 = s1.replaceAll("[T|t][H|h][A|a][T|t]'[S|s]", "That is");
Details:
http://java.sun.com/j2se/1.5.0/docs/...x/Pattern.html
Jun 10 '07 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Vibha Tripathi | last post by:
Hi Folks, I put a Regular Expression question on this list a couple days ago. I would like to rephrase my question as below: In the Python re.sub(regex, replacement, subject)...
5
by: Bradley Plett | last post by:
I'm hopeless at regular expressions (I just don't use them often enough to gain/maintain knowledge), but I need one now and am looking for help. I need to parse through a document to find a URL,...
10
by: Lee Kuhn | last post by:
I am trying the create a regular expression that will essentially match characters in the middle of a fixed-length string. The string may be any characters, but will always be the same length. In...
18
by: Q. John Chen | last post by:
I have Vidation Controls First One: Simple exluce certain special characters: say no a or b or c in the string: * Second One: I required date be entered in "MM/DD/YYYY" format: //+4 How...
5
by: Ryan | last post by:
HELLO I am using the following MICROSOFT SUGGESTED (somewhere on msdn) regular expression to validate email addresses however I understand that the RFP allows for "+" symbols in the email address...
7
by: norton | last post by:
Hello, Does any one know how to extact the following text into 4 different groups(namely Date, Artist, Album and Quality)? - Artist - Album Artist - Album - Artist - Album - Artist -...
7
by: Billa | last post by:
Hi, I am replaceing a big string using different regular expressions (see some example at the end of the message). The problem is whenever I apply a "replace" it makes a new copy of string and I...
6
by: Ludwig | last post by:
Hi, i'm using the regular expression \b\w to find the beginning of a word, in my C# application. If the word is 'public', for example, it works. However, if the word is '<public', it does not...
3
by: Zach | last post by:
Hello, Please forgive if this is not the most appropriate newsgroup for this question. Unfortunately I didn't find a newsgroup specific to regular expressions. I have the following regular...
25
by: Mike | last post by:
I have a regular expression (^(.+)(?=\s*).*\1 ) that results in matches. I would like to get what the actual regular expression is. In other words, when I apply ^(.+)(?=\s*).*\1 to " HEART...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.