Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Regex doesn't recognize single quote

Reply
Thread Tools

Regex doesn't recognize single quote

 
 
Jerric
Guest
Posts: n/a
 
      01-06-2012
Hi, I need to remove special characters, except \w and single quotes,
from a string, can someone please help me on the regex?

for example, I have "ab'de+fg", I want to get "ab'defg", and I tried
the following code, but it removed single quote. seems to me java
cannot handle the pattern like [^'].

String val = "ab'de+fg";
val = val.replaceAll("[^\\w']+", "");

Thanks a lot,
 
Reply With Quote
 
 
 
 
Martin Gregorie
Guest
Posts: n/a
 
      01-06-2012
On Fri, 06 Jan 2012 14:08:49 -0800, Jerric wrote:

> Hi, I need to remove special characters, except \w and single quotes,
> from a string, can someone please help me on the regex?
>
> for example, I have "ab'de+fg", I want to get "ab'defg", and I tried the
> following code, but it removed single quote. seems to me java cannot
> handle the pattern like [^'].
>
> String val = "ab'de+fg";
> val = val.replaceAll("[^\\w']+", "");
>

Did you try escaping the single quote?


--
martin@ | Martin Gregorie
gregorie. | Essex, UK
org |
 
Reply With Quote
 
 
 
 
Daniel Pitts
Guest
Posts: n/a
 
      01-06-2012
On 1/6/12 2:08 PM, Jerric wrote:
> Hi, I need to remove special characters, except \w and single quotes,
> from a string, can someone please help me on the regex?
>
> for example, I have "ab'de+fg", I want to get "ab'defg", and I tried
> the following code, but it removed single quote. seems to me java
> cannot handle the pattern like [^'].
>
> String val = "ab'de+fg";
> val = val.replaceAll("[^\\w']+", "");
>
> Thanks a lot,

It works for me, which indicates the problem is somewhere in the code
you didn't post. Here is an SSCCE:

public class Works {
public static void main(String[] args) {
String val = "ab'de+fg";
System.out.println(val.replaceAll("[^\\w']+", ""));

}
}

Try posting exactly the code which causes the problem.
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      01-07-2012
On Fri, 6 Jan 2012 14:08:49 -0800 (PST), Jerric <(E-Mail Removed)>
wrote, quoted or indirectly quoted someone who said :

>Hi, I need to remove special characters, except \w and single quotes,
>from a string, can someone please help me on the regex?


That is not what a regex is for. Just use a StringBuilder the length
of your String. Then loop through the chars with charAt. If the
character is a ' or \w, ignore it, else append. If it gets complex,
use a switch or if it gets really complicated use a BitSet.


--
Roedy Green Canadian Mind Products
http://mindprod.com
If you can't remember the name of some method,
consider changing it to something you can remember.

 
Reply With Quote
 
Stefan Ram
Guest
Posts: n/a
 
      01-07-2012
Roedy Green <(E-Mail Removed)> writes:
>That is not what a regex is for.


How do you know what it is for?

>Just use a StringBuilder the length of your String. Then
>loop through the chars with charAt. If the character is a
>' or \w, ignore it, else append. If it gets complex, use a
>switch or if it gets really complicated use a BitSet.


This might be needless (as far as we know right now)
optimization bloating the code reducing its readability and
low-level thinking, which might be required sometimes, but
does not serve as a general rule. Still it is nice to know
how it could be done if required.

 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      01-07-2012
On 7 Jan 2012 11:42:26 GMT, http://www.velocityreviews.com/forums/(E-Mail Removed)-berlin.de (Stefan Ram) wrote,
quoted or indirectly quoted someone who said :

>>That is not what a regex is for.

>
> How do you know what it is for?


Regexes are for searching for patterns. Transforming or deleting
characters is much simpler done with a for loop.

How do I know what a regex is for? I am familiar with the API. I have
attempted to use them for various purposes and discovered they were
suitable for some and not for others.
>
>>Just use a StringBuilder the length of your String. Then
>>loop through the chars with charAt. If the character is a
>>' or \w, ignore it, else append. If it gets complex, use a
>>switch or if it gets really complicated use a BitSet.

>
> This might be needless (as far as we know right now)
> optimization bloating the code reducing its readability and
> low-level thinking, which might be required sometimes, but
> does not serve as a general rule. Still it is nice to know
> how it could be done if required.


What is your simpler implementation?

/** remove ' and \w from string
* @param s string to process
* @return string without ' or \w
*/
private static String scrunch( final String s )
{
final Stringbuilder sb = new StringBuilder( s.length() );
for (int i=0; i<s.length(); i++ )
{
char c = s.charAt(i);
if ( !( c = '\'' || c = '\w' ) )
{
sb.append ( c );
}
}
return sb.toString();
}
--
Roedy Green Canadian Mind Products
http://mindprod.com
If you can't remember the name of some method,
consider changing it to something you can remember.

 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      01-07-2012
On 7 Jan 2012 11:42:26 GMT, (E-Mail Removed)-berlin.de (Stefan Ram) wrote,
quoted or indirectly quoted someone who said :

> How do you know what it is for?


I see what you mean. I saw the problem as the pattern translation of
various characters to various other characters. The problem is
actually simpler than that. It translates various different
characters all to the same empty "character".

I find the replace methods dangerous. They are improperly named and
thus it is easy to accidentally use a regex or non-regex. They also
have to compile the pattern every time. I tend to avoid them.
--
Roedy Green Canadian Mind Products
http://mindprod.com
If you can't remember the name of some method,
consider changing it to something you can remember.

 
Reply With Quote
 
Rafael Villar
Guest
Posts: n/a
 
      01-07-2012
On 08/01/12 08:41, Roedy Green wrote:
> On 7 Jan 2012 11:42:26 GMT, (E-Mail Removed)-berlin.de (Stefan Ram) wrote,
> quoted or indirectly quoted someone who said :
>
>>> That is not what a regex is for.

>>
>> How do you know what it is for?

>
> Regexes are for searching for patterns. Transforming or deleting
> characters is much simpler done with a for loop.
>
> How do I know what a regex is for? I am familiar with the API. I have
> attempted to use them for various purposes and discovered they were
> suitable for some and not for others.
>>
>>> Just use a StringBuilder the length of your String. Then
>>> loop through the chars with charAt. If the character is a
>>> ' or \w, ignore it, else append. If it gets complex, use a
>>> switch or if it gets really complicated use a BitSet.

>>
>> This might be needless (as far as we know right now)
>> optimization bloating the code reducing its readability and
>> low-level thinking, which might be required sometimes, but
>> does not serve as a general rule. Still it is nice to know
>> how it could be done if required.

>
> What is your simpler implementation?
>
> /** remove ' and \w from string
> * @param s string to process
> * @return string without ' or \w
> */
> private static String scrunch( final String s )
> {
> final Stringbuilder sb = new StringBuilder( s.length() );
> for (int i=0; i<s.length(); i++ )
> {
> char c = s.charAt(i);
> if ( !( c = '\'' || c = '\w' ) )
> {
> sb.append ( c );
> }
> }
> return sb.toString();
> }


In most cases is better to use a StringBuilder to perform replacements,
but in this particular case String.replaceAll() is better. By the way,
the escape sequence \w is not a java regular escape sequence but belongs
to the pattern syntax (although you should already know about it, as you
say you are familiar with the API).

Anyway a simpler implementation (and one which works, because yours
doesn't):

/** remove ' and \w from string
* @param s string to process
* @return string without ' or \w
*/
private static String scrunch( final String s ) {
return s.replaceAll("[^'\\w]+", "");
}

 
Reply With Quote
 
Rafael Villar
Guest
Posts: n/a
 
      01-07-2012
Mea Culpa, Sorry, it seems Roedy didn't understand the original problem,
and also I didn't understand what Roedy was understanding (sorry Roedy)

Anyway, a simpler method that does what Roedy intends to do:

/** remove ' and \w from string
* @param s string to process
* @return string without ' or \w
*/
private static String scrunch( final String s ) {
return s.replaceAll("['\\w]+", "");
}

However the original problem is unknown as the original code is actually
working.
 
Reply With Quote
 
Stefan Ram
Guest
Posts: n/a
 
      01-07-2012
Roedy Green <(E-Mail Removed)> writes:
>What is your simpler implementation?
>/** remove ' and \w from string
> * @param s string to process
> * @return string without ' or \w
> */
>private static String scrunch( final String s )
>{
>final Stringbuilder sb = new StringBuilder( s.length() );
>for (int i=0; i<s.length(); i++ )
> {
> char c = s.charAt(i);
> if ( !( c = '\'' || c = '\w' ) )


Even with »==« instead of »=« (a »final « in front of the
»char c« should help to detect such errors) and »\\w«
instead of »\w« (»\w« is an illegal escape character in
Java), the comparison with '\\w' is not what the OP actually
wanted.

Maybe you just want other people not to use regular
expressions because you personally can't read them, but why
should your personal knowledge (which is a by-product of you
personal history) be a limitation of anyones else's work?

> {
> sb.append ( c );
> }
> }
>return sb.toString();
>}


static String scrunch( final String s )
{ final java.lang.String string = s.toString();
final java.lang.String result = s.replaceAll( "('|\\\\w)", "" );
return new String( result ); }

(Assuming the class »String« has an appropriate constructor.)

(This implements your documentation, not what the OP wanted.)

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
problem with single-quote and double-quote when using subprocess. Evan Python 3 11-04-2008 12:24 PM
problem with quote and single-quote when using "subprocess" Evan Python 1 11-04-2008 12:08 AM
How to handling string contains single quote and double quote vikrant Perl Misc 8 05-17-2007 04:37 PM
Datagrid on load; replace all double single quote to single quote to display to user Eric Layman ASP .Net 3 04-14-2007 07:16 AM
Single Quote Versus Double Quote In A href link knee-dragger@hotmail.com HTML 3 06-13-2006 12:42 AM



Advertisments