Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- String line = "a=b c='123 456' d=777 e='uij yyy'";
- String [] words = line.split(" ");
- a=b
- c='123 456'
- d=777
- e='uij yyy';
- a=b abc='123 456' &=777 #='uij yyy' ABC='slk slk' 123sdkljhSDFjflsakd@*#&=456sldSLKD)#(
- a=b
- abc='123 456'
- &=777
- #='uij yyy'
- ABC='slk slk'
- 123sdkljhSDFjflsakd@*#&=456sldSLKD)#(
- import java.util.ArrayList;
- import java.util.List;
- import java.util.regex.Matcher;
- import java.util.regex.Pattern;
- public class RegexTest {
- // SPACE CHARACTER followed by
- // sequence of non-space characters of 1 or more followed by
- // first occuring EQUALS CHARACTER
- final static String regex = " [^ ]+?=";
- // static pattern defined outside so that you don't have to compile it
- // for each method call
- static final Pattern p = Pattern.compile(regex);
- public static List<String> tokenize(String input, Pattern p){
- input = input.trim(); // this is important for "last token case"
- // see end of method
- Matcher m = p.matcher(input);
- ArrayList<String> tokens = new ArrayList<String>();
- int beginIndex=0;
- while(m.find()){
- int endIndex = m.start();
- tokens.add(input.substring(beginIndex, endIndex));
- beginIndex = endIndex+1;
- }
- // LAST TOKEN CASE
- //add last token
- tokens.add(input.substring(beginIndex));
- return tokens;
- }
- private static void println(List<String> tokens) {
- for(String token:tokens){
- System.out.println(token);
- }
- }
- public static void main(String args[]){
- String test = "a=b " +
- "abc='123 456' " +
- "&=777 " +
- "#='uij yyy' " +
- "ABC='slk slk' " +
- "123sdkljhSDFjflsakd@*#&=456sldSLKD)#(";
- List<String> tokens = RegexTest.tokenize(test, p);
- println(tokens);
- }
- }
- line.split(" (?=[a-z+]=)")
- a=b
- c='123 456'
- d=777
- e='uij yyy'
- String s = "Ta=b c='123 456' d=777 e='uij yyy'";
- StreamTokenizer st = new StreamTokenizer(new StringReader(s));
- st.ordinaryChars('0', '9');
- st.wordChars('0', '9');
- while (st.nextToken() != StreamTokenizer.TT_EOF) {
- switch (st.ttype) {
- case StreamTokenizer.TT_NUMBER:
- System.out.println(st.nval);
- break;
- case StreamTokenizer.TT_WORD:
- System.out.println(st.sval);
- break;
- case '=':
- System.out.println("=");
- break;
- default:
- System.out.println(st.sval);
- }
- }
- Ta
- =
- b
- c
- =
- 123 456
- d
- =
- 777
- e
- =
- uij yyy
- String line = "a=b c='123 456' d=777 e='uij yyy'";
- Matcher m = Pattern.compile("('[^']*?'|\S)+").matcher(line);
- while (m.find()) {
- System.out.println(m.group()); // or whatever you want to do
- }
- public static void main(String[] args) {
- String token;
- String value="";
- HashMap<String, String> attributes = new HashMap<String, String>();
- String line = "a=b c='123 456' d=777 e='uij yyy'";
- StringTokenizer tokenizer = new StringTokenizer(line," ");
- while(tokenizer.hasMoreTokens()){
- token = tokenizer.nextToken();
- value = token.contains("'") ? value + " " + token : token ;
- if(!value.contains("'") || value.endsWith("'")) {
- //Split the strings and get variables into hashmap
- attributes.put(value.split("=")[0].trim(),value.split("=")[1]);
- value ="";
- }
- }
- System.out.println(attributes);
- }
- java.util.StringTokenizer tokenizer = new java.util.StringTokenizer(line, " ");
- while (tokenizer.hasMoreTokens()) {
- String token = tokenizer.nextToken();
- int index = token.indexOf('=');
- String key = token.substring(0, index);
- String value = token.substring(index + 1);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement