---
title: "正则表达式解析select与from之间"
author: "helloworlda"
date: "2015-06-30T14:48:25Z"
tags: []
categories: ["研发管理"]
slug: "select-from"
draft: false
lastmod: "2015-07-08T10:34:59Z"
id: 2223115
url: "https://helloworlda.iteye.com/blog/2223115"
---

解析sql

public static void main(String\[] args) {  
   String str="select id as a1  , locationid as a2, user\_sex as a3 from";  
   Pattern p= Pattern.compile("[\\\\w+\\\\s+as\\\\s+\\\\w+");//]()只兼容英文

 Pattern p= Pattern.compile("[\\\\w+\\\\s+as\\\\s+\[\\\\w\[\\u4E00-\\u9FFF\]\]+");//]()兼容中英文  
   Matcher m= p.matcher(str);  
   while(m.find()){  
   String tt=m.group();  
   //System.out.println(tt);  
   String\[] sry= tt.split("[\\\\s+as\\\\s]()+");  
  // String\[] sry=tt.split("as");  
    
   System.out.println(sry\[0]+sry\[1]);  
   }

 }