Matching quoted string with regexp

Status
Nicht offen für weitere Antworten.
U

Utez

Gast
I'm using 1.4.2
In a string like

Code:
blah blah "blah @blah" blah blah @blah "blah @ blah" blah

I need to get rid of @ char ONLY in quoted parts
How can I do this with regexps ?

Thanks

[/code]
 

foobar

Top Contributor
I can match the given string, but dont't know how to replace it:
Code:
String input = "foo \"bar @ baz\"  @quux \"bar2 @ baz2\" quuux";
        Pattern p    = Pattern.compile(".*?(?<=\")(.*?@.*?)(?=\").*?");
        Matcher m  = p.matcher(input);
        
        if (m.matches())
        {
            for (int i=1; i <= m.groupCount(); i++)
            {
                System.out.println("found " + m.group(i));
            }   
        }

What exactly are u trying to do?
 
U

Utez

Gast
to foobar

Actually i have to modify some custom SQL statement in a way it does not contain dots in aliases. Something like

Code:
...
T1.field1 AS "Field.1", T1.field2 AS "Field.2", T2.field1 AS "Field.1",
...

I need to make it look like

Code:
...
T1.field1 AS "Field1", T1.field2 AS "Field2", T2.field1 AS "Field1",
...

So i have no match only those dots that are inside of strings
And I've got no idea how do I do this in a more of less simple way
 

André Uhres

Top Contributor
Code:
       String str = "T1.field1 AS \"Field.1\", T1.field2 AS \"Field.2\", T2.field1 AS \"Field.1\"," ;
        StringBuffer resultString = new StringBuffer();
        Pattern regex = Pattern.compile("\\B\"\\b([^\"]+)\\b\"\\B");
        Matcher regexMatcher = regex.matcher(str);
        while (regexMatcher.find()) {
            String[] x = regexMatcher.group().split("\\.");
            regexMatcher.appendReplacement(resultString, x[0]+x[1]);
        }
        regexMatcher.appendTail(resultString);
 
G

Guest

Gast
Case insensitive: as ("(\w+\.\p{Digit}+)")
Case sensitive: (as|AS) ("(\w+\.\p{Digit}+)")

Example
Code:
String str = "T1.field1 AS \"Field.1\", T1.field23 AS \"Field.23\", T2.field1 AS \"Field.1\",";
Matcher matcher = Pattern.compile("as (\"(\\w+\\.\\p{Digit}+)\")", Pattern.CASE_INSENSITIVE).matcher(str);
StringBuffer buffer = new StringBuffer(str.length());
while(matcher.find()) {
   matcher.appendReplacement(buffer, matcher.group(0).replace(".", ""));
}
matcher.appendTail(buffer);
System.out.println(buffer.toString());
 
G

Guest

Gast
@André
Your code will fail, if an alias does no contain a dot. :lol:

AS "Whatever" for example.


This one will work
Code:
Matcher matcher = Pattern.compile("as\\s*\"(\\w+\\.)+\\w+\"", Pattern.CASE_INSENSITIVE).matcher(str);
StringBuffer buffer = new StringBuffer(str.length());
while(matcher.find()) {
   matcher.appendReplacement(buffer, matcher.group(0).replace(".", ""));
}
matcher.appendTail(buffer);
 

André Uhres

Top Contributor
Anonymous hat gesagt.:
@André
Your code will fail, if an alias does no contain a dot..
1. My code can easily be adapted to meet your additional requirement which isn't a requirement of Utez.

2. Originally, my code was intended for the very first example in this thread (Matching quoted string with regexp)
which doesn't contain keywords like 'AS'. Thus, my code is more generally useable.

3. Your code won't work with Java 4, which is a requirement of Utez.
 
G

Guest

Gast
André Uhres hat gesagt.:
Anonymous hat gesagt.:
@André
Your code will fail, if an alias does no contain a dot..
1. My code can easily be adapted to meet your additional requirement which isn't a requirement of Utez.

2. Originally, my code was intended for the very first example in this thread (Matching quoted string with regexp)
which doesn't contain keywords like 'AS'. Thus, my code is more generally useable.

3. Your code won't work with Java 4, which is a requirement of Utez.
OK, reg dich nicht auf. :wink:
Mein Code funktioniert aber auch mit JDK 1.4.2. Wie auch immer, er hat jetzt
paar Beispiele, um damit zu experimentieren.

@Utez
Take a look at the QuickREx Plugin for Eclipse.
http://www.bastian-bergerhoff.com/eclipse/features
It's pretty useful for testing of regular expresions.
 
G

Guest

Gast
André Uhres hat gesagt.:
Anonymous hat gesagt.:
...
Anonymous hat gesagt.:
Mein Code funktioniert aber auch mit JDK 1.4.2. ..
Aus der API Dukomentation:

public String replace(CharSequence target, CharSequence replacement)
...
Since:
1.5
Ätch :shock:
Wenn man in Eclipse "Compiler compliance level" auf 1.4 setzt, wird es trotzdem compiliert.
Das andere 1.5er Zeug wie Generics etc. aber nicht. Du hast recht, in der API steht, dass es
erst seit JDK 1.5 verfügbar ist. Komisch... ???:L Man muss den Compile komplett auf 1.4
umstellen, um Fehler zu erkennen.
 
Status
Nicht offen für weitere Antworten.

Ähnliche Java Themen

Neue Themen


Oben