Reg Exp

B

bajackso

I'm trying to split a string that looks like this


string1/string2/*/stringn:moreStrings

/stringn: will only occur once!

I've tried a bunch of expressions, but this is the I'm currently using:

\\\\\\w+\\:

\ literal(that's why there is 4 \), \w+ all characters any number of
times, followed by a : literal. It'll find string2 and :moreStrings.
Help!
 
S

shakah

I'm trying to split a string that looks like this


string1/string2/*/stringn:moreStrings

/stringn: will only occur once!

I've tried a bunch of expressions, but this is the I'm currently using:

\\\\\\w+\\:

\ literal(that's why there is 4 \), \w+ all characters any number of
times, followed by a : literal. It'll find string2 and :moreStrings.
Help!

I really can't figure out what you're asking. Are you saying that your
input lines will look like the following:

aaaaaa/bb/cccc/dd/eeeee:blah blah blah
ffffff/gggg:foo foo foo

and that you want to break them into
{ "aaaaaa", "bb", "cccc", "dd", "eeeee", "blah blah blah" }
and
{ "ffffff", "gggg", "foo foo foo" }
?
 
B

bajackso

Sorry, I was at work and we were fixing to leave so I was rushed to
type that up.

Here is an example of the input:
path:\string1\string2\...\stringn:<a letter><some more paramaters that
may contain a :>

Now, the only string I want to get from that input is stringn. I can
gurantee that there will only ever be once occurance of something that
looks like \[a-zA-Z]: THe problem is I cannot get any of the
expressions I write for the splite string function to work. The one I
currantly have(its about the 8th iteration of it) looks like this:

\\\\\\w+\\: Which, as far as I can tell, means this:
the first two \\ are for a literal followed by the next two for a
literal \(this should find a backslash). Then look for all at least 1
character but it doesn't matter how many there are with \w+, then look
for a literal : using \:. Hope that makes more sense.

I don't understand why when I do this its not returning stringn, but it
returns stringn-1 and the letter after the colon. Thanks
 
S

shakah

Sorry, I was at work and we were fixing to leave so I was rushed to
type that up.

Here is an example of the input:
path:\string1\string2\...\stringn:<a letter><some more paramaters that
may contain a :>

Now, the only string I want to get from that input is stringn. I can
gurantee that there will only ever be once occurance of something that
looks like \[a-zA-Z]: THe problem is I cannot get any of the
expressions I write for the splite string function to work. The one I
currantly have(its about the 8th iteration of it) looks like this:

\\\\\\w+\\: Which, as far as I can tell, means this:
the first two \\ are for a literal followed by the next two for a
literal \(this should find a backslash). Then look for all at least 1
character but it doesn't matter how many there are with \w+, then look
for a literal : using \:. Hope that makes more sense.

I don't understand why when I do this its not returning stringn, but it
returns stringn-1 and the letter after the colon. Thanks

Can you use Java's regex classes instead of split()?

Looking for '\\([a-zA-Z]+):' and taking the first group's content seems
to work:

public class regextest2 {
public static void main(String [] asArgs) {
java.util.regex.Pattern p =
java.util.regex.Pattern.compile(asArgs[0]) ;
for(int nArg=1; nArg<asArgs.length; ++nArg) {
System.out.println("looking in '" + asArgs[nArg] + "'") ;
java.util.regex.Matcher m = p.matcher(asArgs[nArg]) ;
if(!m.find()) {
System.out.println(" not found") ;
}
else {
System.out.println(" found: '" + m.group(1) + "'") ;
}
}
}
}

jc@sarah:~/tmp$ /usr/java/jdk1.5.0_01/bin/javac regextest2.java
jc@sarah:~/tmp$ /usr/java/jdk1.5.0_01/bin/java regextest2
'\\([a-zA-Z]+):' "path:\string1\string2\...\stringn:<a letter><may
contain a :>"
looking in 'path:\string1\string2\...\stringn:<a letter><may contain a
:>'
found: 'stringn'
 

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

Forum statistics

Threads
473,756
Messages
2,569,534
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top