Jason
Jason

Reputation: 1307

I am having trouble with regex, I am not sure why it does not work

Can anyone help me convert this regex into Java? I am not sure why it is not working, I have read the documentation and made it for Java, but it doesn't work with Java. However, it works on a Perl regex testing website just fine.

(.*?);[01]:

And I basically have this:

expiem;0:2pfemfrance;1:runiora;1:stallker420;1:phoenixblaze0916;1:myothorax;1

All I want to do is get a list of the names expiem, 2pfemfrance, etc. into a string array

Yes, here is my code: where the builder.toString() contains what I mentioned

Pattern pattern = Pattern.compile("h=(.*)");
Matcher match = pattern.matcher(builder.toString());
if( match.find() ) {
    this.userlist = match.group(1).split("(.*?);[01]:");                              
    this.loaded = true;
    this.index = 0;
}   

By the way, match.group(1) is the exact string I posted, it is exactly

expiem;0:2pfemfrance;1:runiora;1:stallker420;1:phoenixblaze0916;1:myothorax;1

(I tested it by printing it out on the console)

Upvotes: 0

Views: 97

Answers (4)

Bohemian
Bohemian

Reputation: 424983

Two problems:

  • your regex is capturing your target - the regex should be for the separators, not the content you want to keep
  • you have waaay too much code. You only need one line!

Try this:

String[] names = builder.toString().split(";[01]:");

Upvotes: 0

Klas Lindbäck
Klas Lindbäck

Reputation: 33273

You don't need the string capture to be part of your split expression: it will eat up your string.

You state that the perl version works, however that requires that the input string ends with a :. If it doesn't, you need to add a ? after to the : to specify that it is optional.

Try:

this.userlist = match.group(1).split(";[01]:?");

Upvotes: 2

Andreas Fester
Andreas Fester

Reputation: 36630

With this code

String input = "h=expiem;0:2pfemfrance;1:runiora;1:stallker420;1:phoenixblaze0916;1:myothorax;1";

Pattern pattern = Pattern.compile("h=(.*)");
Matcher match = pattern.matcher(input);
if( match.find() ) {
   String substr = match.group(1);
   System.out.println(substr);

   String[] userlist = substr.split(";[01]:?");
   System.out.println(Arrays.toString(userlist));
}   

you get

expiem;0:2pfemfrance;1:runiora;1:stallker420;1:phoenixblaze0916;1:myothorax;1
[expiem, 2pfemfrance, runiora, stallker420, phoenixblaze0916, myothorax]

The relevant regexp to split the string is ";[01]:?"

Upvotes: 1

Marc
Marc

Reputation: 6190

String names = "expiem;0:2pfemfrance;1:runiora;1:stallker420;1:phoenixblaze0916;1:myothorax;1"
String[] nameArray = names.split(":");

List<String> nameList = new ArrayList<String>();
for (String name : nameArray) {
    String[] tupel = name.split(";");
    nameList.add(tupel[0]);
}

Well it is not a cool regex solution but imo easy to understand. Basically you split the long string into smaller strings which are separated by :

Then you split the small strings using the separator ; and add the first entry of that result (which is the name) to a list.

Upvotes: 0

Related Questions