473,464 Members | 1,501 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Java split regex

Hello,

I want to split a string starting from ${ and ending with }
where anythign can be inbetween ( ${*} )

I have tried
Expand|Select|Wrap|Line Numbers
  1. String url2 = "http://someurl${language}somemore.com";
  2.  String[] url3 = url2.split("\\$\\{\\w\\}");
  3.  
However I am getting no luck with it,
help would be greatly appreciated
Sep 30 '10 #1
7 2863
Oralloy
988 Expert 512MB
FreshRob,

I think you'd be better off walking the string and extracting each segment in turn using a recognizer pattern, but it all depends on the language you're trying to implement.

If you just want to do the split, try something like:
Expand|Select|Wrap|Line Numbers
  1. String url3[] = url2.split("(\\$\\{)|(\\})");
The problem with what you're doing, is that it will split strings like this:
Expand|Select|Wrap|Line Numbers
  1. String urlBad = "http://${insertOne${insertTwo}more}";
into:
Expand|Select|Wrap|Line Numbers
  1. String urlBadBits[] = {"http://", "insertOne", "insertTwo", "more"};
Which is probably a bad thing.
Sep 30 '10 #2
Hello Oralloy,

Thank you for your reply.
Lookign at it from your perspective I can see where I am making the mistake

I believe what I really require is to split the string before ${ and then split it after }
where
url3[0] = http://someurl
url3[1] = ${language}
url3[2] = somemore.com
Sep 30 '10 #3
Oralloy
988 Expert 512MB
Right,

So if you were to use the pattern class to break up the string into the three pieces, what sort of pattern would you consider using?

What I'm guiding you towards is a solution where you iteratively process the string and work off all your substitutions. Am I making sense?
Sep 30 '10 #4
I should probably give some background to my problem.

The users pass a string with a url
e.g. "http://www.somedomain.com
within the url they set some parameters ${language}

The code then searches the code finds these strings and replaces them with the correct variable.

Because its going to be dynamic, I require the code to see if the parameter exists and if so replaces it.

I believe he premise of what I was trying to do is illogical and not feasible.
Sep 30 '10 #5
Oralloy
988 Expert 512MB
FreshRob,

The problem is at what point do you stop doing this replacement strategy. The code you want is actually fairly straightforward to implement, just tedious.

Also, error handling is going to be a pain, as well.

Basically all you need to do is repeatedly recognize against the head of the string, and strip off the part that you've just done, adding it to an output string buffer.

What happens is that users put in strange forms, like the one I showed in the error case of my first post.

So....some basic pseudo-Java code, because I don't have time to write and debug this:
Expand|Select|Wrap|Line Numbers
  1. String interpolate(String input, Map<String, String>vars)
  2. {
  3.   String in = input;
  4.   StringBuilder out = new StringBuilder();
  5.  
  6.   String patVar = "(\\$\\{\\w+\\})";
  7.   String patStr = "([^$]+)";
  8.   Pattern p = Pattern.compile("^(" + patStr + "|" +patVar + ")");
  9.  
  10.   int limit = input.length();
  11.   while(not in.equals(""))
  12.   {
  13.     Matcher m = p.matcher(in);
  14.     if (not m.matches())
  15.       throw new Error("Bad input string fella.");
  16.  
  17.     String bit = m.group();     // grab the matched bit
  18.  
  19.     int bitLen = bit.length();  // chop off the input bits
  20.     in = in.substring(bitLen);
  21.  
  22.     if (bit.charAt(0) != '$')
  23.       out.append(bit);
  24.     else if (vars.comtainsKey(bit.substring(2,bitLen-1))
  25.       out.append(vars(bit.substring(2,bitLen-1));
  26.     else
  27.       throw new Error("what is this useless bit: \"" + bit + "\"?");
  28.   }
  29. }
  30.  
Sep 30 '10 #6
Thank you very much this was a lot of help
Oct 1 '10 #7
Oralloy
988 Expert 512MB
FreshRob,

Glad to help.

Cheers!
Oct 4 '10 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: David | last post by:
I need to write a regular expression to group lines of text separated by a blank line. Ex. Input: line1 line2 line3
5
by: Blue Ocean | last post by:
In short, it's not working right for me. In long: The program is designed to read numbers from an accumulator and speak them out loud. Unfortunately, the class that contains the method to...
5
by: Carlitos | last post by:
Hi there, What would be the counterpart for Java's StringTokenizer class in .NET? In fact, does anybody know a good web site that gives this kind of information, like a Java-.NET/.NET-Java...
3
by: Rico | last post by:
If there are consecutive occurrences of characters from the given delimiter, String.Split() and Regex.Split() produce an empty string as the token that's between such consecutive occurrences. It...
24
by: garyusenet | last post by:
I'm working on a data file and can't find any common delimmiters in the file to indicate the end of one row of data and the start of the next. Rows are not on individual lines but run accross...
14
by: tom t/LA | last post by:
Here is a function to convert a CSV file to a Javascript array. Uses idealized file reading functions based on the std C library, since there is no Javascript standard. Not fully tested. ...
12
blazedaces
by: blazedaces | last post by:
Hello again. I'm trying to take as an input an ArrayList<String> and utilize String's .spit(delimiter) method to turn that into a String. I'm getting some kind of error though (I'll post the code...
10
by: Mokita | last post by:
Hello, I am working with Taverna to build a workflow. Taverna has a beanshell where I can program in java. I am having some problems in writing a script. I want to extract information from a...
4
by: sukatoa | last post by:
This was my first time to encouter this kind of exception.... that exception appears when i invoked the the method below. private final String encrypting(String enc){ int...
7
jhardman
by: jhardman | last post by:
So I'm trying to use regular expressions, but my compiler keeps failing at import java.util.regex.*; I know that the regex package is supposed to be there, every reference I have found put it right...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.