A question about regular expressions

C

Covington Bradshaw

Hi every one,

I have a basic regular expressions question.
It goes like this: How do I extract a sequence of
@-delimited characters from a text?

Example: @{1}.*?@{1}
Will give:
123456789abcdefhghij
when applied to the following text:
@123456789abcdefghij@987654321stuvwxyz@

However I am trying to build a regular expression
for a more complex case where the double @@, which
is the escape character for @, can also be handled.
Consequently I need to know how to extract

123456789@@abcdefhghij

from

@123456789@@abcdefghij@987654321@@stuvwxyz@

using regular expressions.

Note the expression @([^@]|((@@)*))@ was suggested in a
previous reply but this one returns a false positive
on the following text:

@123456789@@abc

by returning the value:
@123456789@

Anyone has a clue?

Thanks
 
C

Chris Smith

Covington Bradshaw said:
Hi every one,

I have a basic regular expressions question.

Yes. Can you do us a favor and respond in existing threads, instead of
starting new ones all the time? Do you know how to reply to an existing
message?
Note the expression @([^@]|((@@)*))@ was suggested in a
previous reply but this one returns a false positive
on the following text:

@123456789@@abc

Okay. Then try (^|[^@])@([^@]|((@@)*))@($|[^@])

This time, your result will be in group #2.
 
C

Covington Bradshaw

public static void main(String[] args) {
Pattern pattern = Pattern.compile("(^|[^@])@([^@]|((@@)*))@($|[^@])", Pattern.DOTALL);
String b = "@123456789@@abcdefghij@987654321@@stuvwxyz@";
Matcher matcher = pattern.matcher(b);
System.out.println("find [" +matcher.find() + "]");
System.out.println("group [" + matcher.group() + "]");
System.out.println("start [" + matcher.start() + "]");
System.out.println("end [" + matcher.end() + "]");
}

This code returns 9@@a

I am expecting @123456789@@abcdefghij@

Am I missing something?

=======================
 
C

Chris Smith

Covington Bradshaw said:
This code returns 9@@a

I am expecting @123456789@@abcdefghij@

Am I missing something?

Nah. I just screwed up, and didn't realize it. This one is actually
tested:

(^|[^@])@(([^@]|(@@))+)@($|[^@])

Again, answer in group #2.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top