當前位置:首頁 » 編程語言 » 正則表達式提取java

正則表達式提取java

發布時間: 2023-01-08 05:57:24

㈠ 請問怎麼用java正則表達式提取以下文本中指定的內容

//回答完畢,採納即可

importjava.io.File;
importjava.io.FileNotFoundException;
importjava.util.Scanner;
importjava.util.regex.Matcher;
importjava.util.regex.Pattern;

publicclassDrinks
{
publicstaticfinalStringLINE=System.getProperty("line.separator");
publicstaticvoidmain(String[]args)
{
try
{
Stringresult="";
Scannerscanner=newScanner(newFile("data.txt"));
while(scanner.hasNextLine())
{
Stringline=scanner.nextLine();
result+=line+LINE;
}
StringuserRegex="[\s\S]*%user(\s+%\w+){5}[ \s]+([\d\.]+)\s[\s\S]*";
System.out.println("%user想對應的數據:"+result.replaceAll(userRegex,"$2"));
StringutilRegex="sda.*\s([\d\.]+)";
Patternpattern=Pattern.compile(utilRegex);
Matchermatcher=pattern.matcher(result);
while(matcher.find())
{
System.out.println("%util相對應的數據:"+matcher.group(1));
}
StringuseRegex="use:\s*([\d\.%]+)";
pattern=Pattern.compile(useRegex);
matcher=pattern.matcher(result);
while(matcher.find())
{
System.out.println("use相對應的數據:"+matcher.group(1));
}
}
catch(FileNotFoundExceptione)
{
e.printStackTrace();
}
}
}

㈡ java正則表達式如何獲取字元串中所有匹配內容

java正則表達式提取需要用到Matcher類。

㈢ java正則表達式截取字元串

importjava.util.regex.Matcher;
importjava.util.regex.Pattern;

publicclassHello{
publicstaticvoidmain(String[]args){
Stringre="\[([^\]]+)\]";
Stringstr="[您好],abcdefg,[abc]";

Patternp=Pattern.compile(re);
Matcherm=p.matcher(str);
while(m.find()){
System.out.println(m.group(1));
}
}
}

將m.group(1)組建成你自己想要的格式就好了

㈣ 使用java正則表達式提取文件名問題,怎麼解決

用fname舉個例子
StringfName="G:\Java_Source\navigation_tigra_menu\demo1\img\lev1_arrow.gif";

fName=fName.trim();

Stringtemp[]=fName.split("\\");/**split裡面必須是正則表達式,"\"的作用是對字元串轉義*/

//temp[]=[G:,Java_Source,navigation_tigra_menu,demo1,img,lev1_arrow.gif]
System.out.println("temp[]="+Arrays.toString(temp));
fName=temp[temp.length-1];

System.out.println("方法三:fileName="+fName);

㈤ java 怎麼利用正則表達式從給定的字元串中取出匹配規則字元串

利用正則表達式從給定的字元串中取出符合匹配規則的字元串的Java程序如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21

import java.util.regex.Matcher;

import java.util.regex.Pattern;

public class E {

public static void main(String[] args) {

Pattern p = Pattern.compile("[A-Za-z]+");//設定匹配規則為取出字元串中的字母

Matcher m = p.matcher("12sifiwq820aufu");//與字元串匹配

while(m.find()){

System.out.println(m.group());

}

}

}

運行結果:
sifiwq
aufu

㈥ 用java正則表達式提取java程序中的注釋

正則的效率非常低,有很多不用正則的好方法:
一,示例 某java程序
比如說一個Test2.java,將其保存在C盤根目錄下,代碼內容如下
/**
* @author xxx
*
*/
public class Test2 {
/* main method */
public static void main(String[] args) {
//a
int a =5;
//b
int b =5;
//a+b
System.out.println(a+b);

}
}

二,提取注釋程序,將所有注釋放到一個List裡面,最後列印輸出:
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

public class Test {
private static final String javaFilePath = "C:/Test2.java";
public static void main(String[] args) throws IOException {
List<String> comments = new ArrayList<String>();
BufferedReader bfr = new BufferedReader(new InputStreamReader(new FileInputStream(javaFilePath)));
String line=null;
while((line=bfr.readLine())!=null){
line = line.trim();
if(line.startsWith("//")){
comments.add(line);
}
else if(line.startsWith("/*")&&line.endsWith("*/")){
comments.add(line);
}else if(line.startsWith("/*")&&!line.endsWith("*/")){
StringBuffer multilineComment = new StringBuffer(line);
while((line=bfr.readLine())!=null){
line = line.trim();
multilineComment.append("\n").append(line);
if(line.endsWith("*/")){
comments.add(multilineComment.toString());
break;
}
}

}
}
bfr.close();
for(int i=0;i<comments.size();i++){
System.out.println("第"+(i+1)+"處注釋: ");
System.out.println(comments.get(i));
}
}

}

三,輸出結果:
第1處注釋:
/**
* @author xxx
*
*/
第2處注釋:
/* main method */
第3處注釋:
//a
第4處注釋:
//b
第5處注釋:
//a+b

㈦ 怎樣用java正則表達式從文檔中提取需要的信息

給你代碼,在一個文件夾中找那個文件中有人要查找的字元串.

-------------------------------------------------------------------
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Test2 {

public static void main(String args[]) throws Exception {
execute(new File("H:\\test"), "find");
}

public static void execute(File root, String findStr) throws Exception {
File[] files = root.listFiles();
for (int i = 0; i < files.length; i++) {
if (files[i].isDirectory()) {
continue;
}
if (fileHandler(files[i], findStr)) {
System.out.println(files[i].getName());
}
}

}

public static boolean fileHandler(File file, String findStr)
throws Exception {
FileReader fr = new FileReader(file);
BufferedReader br = new BufferedReader(fr);
boolean result = false;
while (br.ready()) {
String line = br.readLine();
result = findCount(line, findStr);
if (result) {
break;
}
}
br.close();
fr.close();

return result;
}

public static boolean findCount(String line, String findStr) {
Pattern p = Pattern.compile(findStr);
Matcher m = p.matcher(line);
int count = 0;
while (m.find()) {
if (!"".equals(m.group())) {
return true;
}
}
return false;
}

}

㈧ JAVA 正則表達式 提取數據

類似這種,就不要用什麼正則表達式了。
直接String[] ss = str.split(",");
Map m= new HashMap();

for(String s:ss){
String [] ts = s.split(":");

m.put(ts[0],ts[1]);

}

然後你想用name屬性,就直接m.get("name");就取到了

㈨ java正則表達式如何獲取字元串中所有匹配內容

java正則表達式如何獲取字元串中所有匹配內容

java正則表達式提取需要用到Matcher類。

正則規則就是「一個數字加上大於等於0個非數字再加上結束符」

Pattern pattern

= Pattern.pile("(\d)[^\d]*$")

Matcher matcher

= pattern.matcher(s)

if(matcher.find())

System.out.println

(matcher.group(1)); } }

如何獲取字元串中匹配到正則表達式的子串開

mport java.util.regex.Matcher;import java.util.regex.Pattern;public class Test {public static void main(String[] args) {String s = "A876X"; 把要匹配的字元串寫成正則表達式,然後要提取的字元使用括弧括起來 在這里,我們要提取最後一個數字,正則規則就是「一個數字加上大於等於0個非數字再加上結束符」Pattern pattern = Pattern.pile("(\d)[^\d]*$");Matcher matcher = pattern.matcher(s);if(matcher.find())System.out.println(matcher.group(1));}}

正則表達式獲取字元串

string pattern=@"<font>(.+?)</font>"
取分組 Match.group[1]

JS如何用正則表達式 獲取字元串內的匹配部份?

實現的效果:在字元串中abcdefgname='test'sddfhskshjsfsjdfps中獲取name的值test
實現的機制:通過replace的回調函數獲取。

可以通過下面的代碼獲取匹配部分

var str = "abcdefgname='test'sddfhskshjsfsjdfps";

var reg = /name='((w|-|s)+)/ig;

str.replace(reg, function() {
console.log(arguments.length); 5
console.log(arguments[1]);test
});

字元串 stringObject 的 replace() 方法執行的是查找並替換的操作。它將在 stringObject 中查找與 regexp 相匹配的子字元串,然後用 replacement 來替換這些子串。如果 regexp 具有全局標志 g,那麼 replace() 方法將替換所有匹配的子串。否則,它只替換第一個匹配子串。

正則表達式如何獲取被匹配字元串的匹配組名

java正則提取需要用到Matcher類,下面給出案例示例供參考
需要提取車牌號中最後一個數字,比如說:蘇A7865提取5,蘇A876X提取6
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Test {
public static void main(String[] args) {
String s = "A876X";
把要匹配的字元串寫成正則表達式,然後要提取的字元使用括弧括起來
在這里,我們要提取最後一個數字,正則規則就是「一個數字加上大於等於0個非數字再加上結束符」
Pattern pattern = Pattern.pile("(\d)[^\d]*$");
Matcher matcher = pattern.matcher(s);
if(matcher.find())
System.out.println(matcher.group(1));
}
}
關於Matcher 中的幾個方法說明:
Mathcer.start()
Matcher.end()
Matcher.group()
當使用matches(),lookingAt(),find()執行匹配操作後,就可以利用以上三個方法得到更詳細的信息.
start()返回匹配到的子字元串在字元串中的索引位置.
end()返回匹配到的子字元串的最後一個字元在字元串中的索引位置.
group()返回匹配到的子字元串
示例代碼如下,具體功能請參考注釋
Pattern p=Pattern.pile(「d+」);
Matcher m=p.matcher(「aaa2223bb」);
m.find();匹配2223
m.start();返回3
m.end();返回7,返回的是2223後的索引號
m.group();返回2223
Mathcer m2=p.matcher(「2223bb」);
m2.lookingAt(); 匹配2223
m2.start(); 返回0,由於lookingAt()只能匹配前面的字元串,所以當使用lookingAt()匹配時,start()方法總是返回0
m2.end(); 返回4
m2.group(); 返回2223
Matcher m3=p.matcher(「2223」); 如果Matcher m3=p.matcher(「2223bb」); 那麼下面的方法出錯,因為不匹配返回false
m3.matches(); 匹配整個字元串
m3.start(); 返回0
m3.end(); 返回3,原因相信大家也清楚了,因為matches()需要匹配所有字元串
m3.group(); 返回2223
另外,Mathcer類中start(),end(),group()均有一個重載方法它們是start(int i),end(int i),group(int i)專用於分組操作,Mathcer類還有一個groupCount()用於返回有多少組.
示例如下:
Pattern p=Pattern.pile(「([a-z]+)(d+)」);
Matcher m=p.matcher(「aaa2223bb」);
m.find(); 匹配aaa2223
m.groupCount(); 返回2,因為有2組
m.start(1); 返回0 返回第一組匹配到的子字元串在字元串中的索引號
m.start(2); 返回3
m.end(1); 返回3 返回第一組匹配到的子字元串的最後一個字元在字元串中的索引位置.
m.end(2); 返回7
m.group(1); 返回aaa,返回第一組匹配到的子字元串
m.group(2); 返回2223,返回第二組匹配到的子字元串
注意: 只有當匹配操作成功,才可以使用start(),end(),group()三個方法,否則會拋出java.lang.IllegalStateException,也就是當matches(),lookingAt(),find()其中任意一個方法返回true時,才可以使用。

C# 正則表達式獲取字元串?

針對你的問題:
<aa>是給數組命名
(?<name>subexpression)
其中name是有效的組名稱,而subexpression是任何有效的正則表達式模式。 name不得包含任何標點符號字元,並且不能以數字開頭。
這個方式相當於下面這個表達式
"(?<!0-9a-zA-Z)([0-9]|[a-z]|[A-Z]){1,}"

java正則表達式如何獲取分組匹配內容

String str = "我的QQ是:456456我的電話是:0532214我的郵箱是:aaa@aaa.";
d 表示0-9 任意一個數字 後面有+號 說明這個0-9單個數位出現一到多次 比如21312314
String reg = "\d+";
Pattern是一個正則表達式經編譯後的表現模式。
Pattern pattern = Pattern.pile (reg);
Matcher 一個Matcher對象是一個狀態機器,它依據Pattern對象做為匹配模式對字元串展開匹配檢查。
Matcher matcher = pattern.matcher (str);
只有執行了find()方法 後,狀態機matcher才是真正開始進行匹配工作的!
while (matcher.find ())
{
matcher.group()返回匹配到的子字元串
System.out.println (matcher.group ());
}
}

Java正則表達式匹配是否存在字元串。

正則表達式為:.*a.*b.*c.*
package .test;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class RegexTest {
/**
* @param args
*/
public static void main(String[] args) {
TODO Auto-generated method stub
String source = "abcdefg " +
"uaibec " +
"wabbcd " +
"adb " +
"acb";
String regex = "(.*a.*b.*c.*)";
Pattern pattern = Pattern.pile(regex,Pattern.MULTILINE);
Matcher matcher = pattern.matcher(source);
while(matcher.find()){
System.out.println(matcher.group());
}
}
}
你可以直接復制出來,運行一下就可以了

㈩ java正則表達式如何獲取字元串中所有匹配內容

一、正則表達式基礎知識

1.1 句點符號

在玩英文拼字游戲,想要找出三個字母的單詞,而且這些單詞必須以「t」字母開頭,以「n」字母結束。另外,假設有一本英文字典,可以用正則表達式搜索全部內容。要構造出這個正則表達式,可以使用一個通配符——句點符號「.」。這樣,完整的表達式就是「t.n」,匹配「tan」、「ten」、「tin」和「ton」,還匹配「t#n」、「tpn」甚至「t n」,還有其許多無意義的組合。這是因為句點符號匹配所有字元,包括空格、Tab字元甚至換行符:

1.3 「或」符號

如果除了上面匹配的所有單詞之外,還想要匹配「toon」,那麼,可以使用「|」操作符。「|」操作符的基本意義就是「或」運算。要匹配「toon」,使用「t(a|e|i|o|oo)n」正則表達式。這里不能使用方擴號,因為方括弧只允許匹配單個字元;這里必須使用圓括弧「()」。圓括弧還可以用來分組。

1.4 表示匹配次數的符號


下表顯示了正則表達式的語法:

表 1.1 正則表達式語法

熱點內容
php入門手冊 發布:2025-07-10 14:42:24 瀏覽:790
手機如何設密碼鎖屏 發布:2025-07-10 14:17:06 瀏覽:803
java求絕對值 發布:2025-07-10 14:10:55 瀏覽:653
usb調試開關在哪裡安卓 發布:2025-07-10 13:59:55 瀏覽:78
資料庫維度 發布:2025-07-10 13:54:31 瀏覽:799
c語言位域的賦值 發布:2025-07-10 13:54:30 瀏覽:583
查成績密碼忘了怎麼辦 發布:2025-07-10 13:52:21 瀏覽:819
java死 發布:2025-07-10 13:51:30 瀏覽:996
車輛設施配置有哪些 發布:2025-07-10 13:42:28 瀏覽:820
java的成員 發布:2025-07-10 13:38:59 瀏覽:720