# Matching quoted string with regexp



## Utez (27. Okt 2006)

I'm using 1.4.2
In a string like 


```
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 (27. Okt 2006)

I can match the given string, but dont't know how to replace it:

```
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?


----------



## Utez (27. Okt 2006)

to foobar

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


```
...
T1.field1 AS "Field.1", T1.field2 AS "Field.2", T2.field1 AS "Field.1",
...
```

I need to make it look like


```
...
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 (27. Okt 2006)

```
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);
```


----------



## Guest (27. Okt 2006)

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

Example
	
	
	
	





```
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());
```


----------



## Guest (27. Okt 2006)

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

AS "Whatever" for example.


This one will work
	
	
	
	





```
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 (28. Okt 2006)

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.


----------



## Guest (28. Okt 2006)

André Uhres hat gesagt.:
			
		

> Anonymous hat gesagt.:
> 
> 
> 
> ...


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.


----------



## André Uhres (28. Okt 2006)

Anonymous hat gesagt.:
			
		

> ..OK, reg dich nicht auf. :wink:


Tu ich auch nicht. Bin ganz ruhig   



			
				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


----------



## Guest (28. Okt 2006)

André Uhres hat gesagt.:
			
		

> Anonymous hat gesagt.:
> 
> 
> 
> ...


----------



## SlaterB (28. Okt 2006)

Oder in die API schauen


----------



## Utez (30. Okt 2006)

Danke allen


----------

