Java extracts information through regular expressions

Java extracts information through regular expressions

Tools are as follows

package com.datafactory.util;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Component
@Slf4j
public class RegexUtils {
    
    
    /**
     * data:原始文案
     * regexPartten:正则表达式
     * */
    public String regex(String data,String regexPartten){
    
    
//        创建正则表达式对象
        Pattern pattern = Pattern.compile(regexPartten);
//        用对象去匹配内容
        Matcher matcher = pattern.matcher(data);
//        执行匹配操作,如果匹配成功,则返回true
        if (matcher.find()) {
    
    
            String ssoInfoValue = matcher.group(1);
//            group(1)只展示被匹配到的内容
            log.info("正则表达式的结果为: " + ssoInfoValue);
//            group(0)返回的信息是带着正则表达式文案的
            log.info("正则表达式的group(0)为:"+matcher.group(0));
            return ssoInfoValue;
        } else {
    
    
            log.info("正则表达式返回结果为空");
            return null;
        }
    }
}

Use and results
The following is the original text of data

[Server:"nginx", Date:"Thu, 13 Jul 2023 08:25:34 GMT", Content-Type:"text/html; charset=UTF-8", Transfer-Encoding:"chunked", Connection:"keep-alive", Vary:"Accept-Encoding", Content-Language:"en-US", Set-Cookie:"ssoInfo=6b1b55697f6206ce8ccc52b46b216a3f; path=/manage_new; expires=Thu, 13-Jul-2023 20:25:34 GMT", "SESSION=b85ac2b7-de5c-42a8-b656-f0502a7646d7; path=/manage_new/; HttpOnly", Access-Control-Allow-Origin:"*", Access-Control-Allow-Credentials:"true", Access-Control-Allow-Methods:"GET, POST, PUT, DELETE, OPTIONS", Access-Control-Allow-Headers:"Authorization,X-CustomHeader,Content-Type,Accept,Origin,User-Agent,DNT,Cache-Control,X-Mx-ReqToken,Keep-Alive,X-Requested-With,If-Modified-Since"]>
		//这里只截取了一部分代码,data即为上面的这个原文,下面的代码用意为从原文中获取cookie以及ssoinfo信息
        String ssoInforegex = "ssoInfo=([a-f0-9]+)";
        String ssoInfo=regexUtils.regex(data,ssoInforegex);
        String cookieregex="SESSION=([a-f0-9\\-]+)";
        String cookie = regexUtils.regex(data,cookieregex);
        cookie = "SESSION="+cookie+";";
        log.info("后台管理系统登录后的cookie为:{},ssoinfo为{}",cookie,ssoInfo);

result

16:25:34.694 [main] INFO  com.datafactory.util.RegexUtils - 正则表达式的结果为: 6b1b55697f6206ce8ccc52b46b216a3f
16:25:34.694 [main] INFO  com.datafactory.util.RegexUtils - 正则表达式的group(0)为:ssoInfo=6b1b55697f6206ce8ccc52b46b216a3f
16:25:34.698 [main] INFO  com.datafactory.util.RegexUtils - 正则表达式的结果为: b85ac2b7-de5c-42a8-b656-f0502a7646d7
16:25:34.698 [main] INFO  com.datafactory.util.RegexUtils - 正则表达式的group(0)为:SESSION=b85ac2b7-de5c-42a8-b656-f0502a7646d7
16:25:34.698 [main] INFO  com.datafactory.service.impl.HTUserServiceImpl - 后台管理系统登录后的cookie为:SESSION=b85ac2b7-de5c-42a8-b656-f0502a7646d7;,ssoinfo为6b1b55697f6206ce8ccc52b46b216a3f

Recommend this website, it looks more intuitive
and regular online
insert image description here
. It is really difficult. How to write this regular expression.
Some expressions are not unusable, but they are not easy to use in java programs. What should I do? I recommend using chatgpt , let him write the expression for you
. The first step is to send the copy first.
insert image description here
The second step is to put forward the demand
insert image description here

Guess you like

Origin blog.csdn.net/qq13933506749/article/details/131705468