android 正则表达式截取字符串问题

weixin_36528687 2018-10-16 03:22:56
 protected void onCreate(Bundle savedInstanceState) {
super.onCreate( savedInstanceState );
setContentView( R.layout.activity_main );
textView = findViewById( R.id.textview );
editText = findViewById( R.id.edittext );

button1 = findViewById( R.id.button );
button1.setOnClickListener( new View.OnClickListener() {
@Override
public void onClick(View view) {

String str = textView.getText().toString();
String regEx = "--sector:\\s*?(\\d+),\\s*block:\\s*\\d+,\\s*key\\s*type:[A-Z],\\s*key\\s*count:\\s*\\d+\\s*Found";
Pattern pattern = Pattern.compile( regEx );
Matcher matcher = pattern.matcher( str );
boolean s = matcher.matches();
int i = (s==true?1:0);
//System.out.print( s );

String str2 = textView.getText().toString();
String regEx2 = "type:([AB]),\\\\s*key\\\\s*count:\\\\s*\\\\d+\\\\s*Found";
Pattern pattern2 = Pattern.compile( regEx2 );
Matcher matcher2 = pattern2.matcher( str2 );
boolean b = matcher2.matches();
int o = (b==true?1:0);



textView.setText("No key specified, trying default keys \n" +
"--sector:13, block: 55, key type:B, key count:13 \n" +
"Found valid key:[ffffffffffff] \n" +
"--sector:14, block: 59, key type:B, key count:13 \n" +
"Found valid key:[ffffffffffff] \n" +
"--sector:15, block: 63, key type:B, key count:13 \n" +
"Found valid key:[ffffffffffff] \n" +
"Found keys have been transferred to the emulator memory \n");

if(-1!= (textView.getText().toString().indexOf("Found keys have been transferred to the emulator memory")))
{
((SectorandKey) getApplication()).setI(textView.getText().toString());
((SectorandKey) getApplication()).setO(textView.getText().toString());

editText.setText("hf mf nested 1 ".toCharArray(),i,o);
}
}


} );

}
}
我是这样写的 为什么正则表达式没有截取到值呢 求大神解答一下 感谢啦
...全文
1194 8 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
YXTS122 2018-11-08
  • 打赏
  • 举报
回复
YXTS122 2018-11-08
  • 打赏
  • 举报
回复
public class MainActivity extends Activity {
	TextView textView,tv,tv3;
	EditText editText;
	Button button1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textView =(TextView) findViewById( R.id.textview );
        tv =(TextView) findViewById( R.id.tv );
        tv3 =(TextView) findViewById( R.id.tv3 );
        editText = (EditText)findViewById( R.id.edittext );
         
        button1 =(Button) findViewById( R.id.button );
        button1.setOnClickListener( new View.OnClickListener() {
            @Override
            public void onClick(View view) {
               try{
                String str = textView.getText().toString();
                String regEx = "--sector:\\s*?(\\d+),\\s*block:\\s*\\d+,\\s*key\\s*type:[A-Z],\\s*key\\s*count:\\s*\\d+\\s*Found";
                Pattern pattern = Pattern.compile( regEx );
                Matcher matcher = pattern.matcher( str );
                boolean s = matcher.matches();
                int i = (s==true?1:0);
                //System.out.print( s );
 
                String str2 = textView.getText().toString();
                String regEx2 = "type:([AB]),\\\\s*key\\\\s*count:\\\\s*\\\\d+\\\\s*Found";
                Pattern pattern2 = Pattern.compile( regEx2 );
                Matcher matcher2 = pattern2.matcher( str2 );
                boolean b = matcher2.matches();
                int o = (b==true?1:0);
 
      
 
                textView.setText("No key specified, trying default keys          \n" +
                        "--sector:13,block: 55, key type:B, key count:13           \n" +
                        "Found valid key:[ffffffffffff]          \n" +
                        "--sector:14,block: 59, key type:B, key count:13           \n" +
                        "Found valid key:[ffffffffffff]          \n" +
                        "--sector:15,block: 63, key type:B, key count:13           \n" +
                        "Found valid key:[ffffffffffff]          \n" +
                        "Found keys have been transferred to the emulator memory          \n");
                int start=str2.indexOf("--sector:")+"--sector:".length();
                
                String data=str2.substring(start, start+2);
 
                if(-1!= (textView.getText().toString().indexOf("Found keys have been transferred to the emulator memory")))
                {
                  /*  ((SectorandKey) getApplication()).setI(textView.getText().toString());
                    ((SectorandKey) getApplication()).setO(textView.getText().toString()); */
 
                    editText.setText("hf mf nested 1 ".toCharArray(),i,o);
                }
                Pattern pattern3 = Pattern.compile("--sector:(.*)block");  
                Matcher m = pattern3.matcher(textView.getText().toString());
                List<String> list= new ArrayList<String>();
                while (m.find()) {
                    list.add(m.group(1));
                }
                Log.e("MainActivity",list.toString());
                tv.setText(list.toString());
                Pattern pattern4 = Pattern.compile("(?<=--sector:)\\d+",Pattern.CASE_INSENSITIVE);  
                Matcher m2 = pattern4.matcher(textView.getText().toString());
                List<String> list2= new ArrayList<String>();
                while (m2.find()) {
                    list2.add(m2.group());
                }
                tv3.setText(list2.toString());
            }
            
            catch (Exception e)
            {
                Toast.makeText(getApplication(),e.toString() ,Toast.LENGTH_LONG).show();
            }
            }
 
 
        } );
    }


   
}
  
  • 打赏
  • 举报
回复

 Pattern pattern = Pattern.compile("(?<=--sector:)\\d+",Pattern.CASE_INSENSITIVE);  
	        Matcher m = pattern.matcher(str);
	        List<String> list= new ArrayList<>();
	        while (m.find()) {
	            list.add(m.group());
	        }
Jing丶無雙 2018-10-18
  • 打赏
  • 举报
回复
引用 2 楼 weixin_36528687 的回复:

	String string="No key specified, trying default keys          \n" +
"--sector:13, block: 55, key type:B, key count:13 \n" +
"Found valid key:[ffffffffffff] \n" +
"--sector:14, block: 59, key type:B, key count:13 \n" +
"Found valid key:[ffffffffffff] \n" +
"--sector:15, block: 63, key type:B, key count:13 \n" +
"Found valid key:[ffffffffffff] \n" +
"Found keys have been transferred to the emulator memory \n";

int start=string.indexOf("--sector:")+"--sector:".length();

String data=string.substring(start, start+2);
雕·不懒惰 2018-10-18
  • 打赏
  • 举报
回复
引用 5 楼 qq_33451426 的回复:
String result = "No key specified, trying default keys \n" +
"--sector:13, block: 55, key type:B, key count:13 \n" +
"Found valid key:[ffffffffffff] \n" +
"--sector:14, block: 59, key type:B, key count:13 \n" +
"Found valid key:[ffffffffffff] \n" +
"--sector:15, block: 63, key type:B, key count:13 \n" +
"Found valid key:[ffffffffffff] \n" +
"Found keys have been transferred to the emulator memory \n";
Pattern pattern = Pattern.compile("--sector:(.*)block");
Matcher m = pattern.matcher(result);
List<String> list= new ArrayList<>();
while (m.find()) {
list.add(m.group(1));
}
LogUtil.e(list);

Log输出 [13, , 14, , 15, ] 然后去掉每个item的都好就是了 //("--sector:(.*),block"); 这样截取的话输出是[],不知道为什么


逗号和block之间空格了一个字符
雕·不懒惰 2018-10-18
  • 打赏
  • 举报
回复
String result = "No key specified, trying default keys \n" +
"--sector:13, block: 55, key type:B, key count:13 \n" +
"Found valid key:[ffffffffffff] \n" +
"--sector:14, block: 59, key type:B, key count:13 \n" +
"Found valid key:[ffffffffffff] \n" +
"--sector:15, block: 63, key type:B, key count:13 \n" +
"Found valid key:[ffffffffffff] \n" +
"Found keys have been transferred to the emulator memory \n";
Pattern pattern = Pattern.compile("--sector:(.*)block");
Matcher m = pattern.matcher(result);
List<String> list= new ArrayList<>();
while (m.find()) {
list.add(m.group(1));
}
LogUtil.e(list);

Log输出 [13, , 14, , 15, ] 然后去掉每个item的都好就是了 //("--sector:(.*),block"); 这样截取的话输出是[],不知道为什么
weixin_36528687 2018-10-16
  • 打赏
  • 举报
回复
"No key specified, trying default keys          \n" +
                        "--sector:13, block: 55, key type:B, key count:13           \n" +
                        "Found valid key:[ffffffffffff]          \n" +
                        "--sector:14, block: 59, key type:B, key count:13           \n" +
                        "Found valid key:[ffffffffffff]          \n" +
                        "--sector:15, block: 63, key type:B, key count:13           \n" +
                        "Found valid key:[ffffffffffff]          \n" +
                        "Found keys have been transferred to the emulator memory          \n"
请问就是这些数据,想要截取Found valid key:[ffffffffffff]上面出现的--sector后面的值,就比如说现在的数据,想要得到13,有什么办法吗
YXTS122 2018-10-16
  • 打赏
  • 举报
回复
断点调试查看一下某个变量的值看看哦

80,471

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧