我正在努力获得正确的REGEX来完成这项任务:
匹配包含特定单词的行的第N个单词
例如:
输入:
this is the first line - blue
this is the second line - green
this is the third line - red
Run Code Online (Sandbox Code Playgroud)
我想匹配包含单词« second » 的第7个单词
期望的输出:
(.*second.*)(?<data>.*?\s){7}(.*)
Run Code Online (Sandbox Code Playgroud)
有谁知道如何做到这一点?
我正在使用http://rubular.com/来测试REGEX.
我已经尝试过这个REGEX 而没有成功 - 它匹配下一行
this is the Foo line - blue
this is the Bar line - green
this is the Test line - red
Run Code Online (Sandbox Code Playgroud)
- - 更新 - -
例2
输入:
this is the first line - blue
this is the …Run Code Online (Sandbox Code Playgroud) 我想在按下Android键盘时获取KeyCode事件的char值.
public void KeyCodeEventAsChar(int keyCode, KeyEvent event) {
char pressedKey;
// TODO: convert key Code Event into char
syslog.d ("TEST", "The pressed key in Android keyboard was char: " + pressedKey);
}
Run Code Online (Sandbox Code Playgroud)
有没有人知道如何做到这一点?!
更新:
我不想要硬编码的文字!我想把它转换为通讯员char!
更新2:
我还需要死的字符,例如:à,á,ò,ó等......
答案发现:
// TODO: convert key Code Event into char
char pressedKey = (char) event.getUnicodeChar();
Run Code Online (Sandbox Code Playgroud)
重要说明: char只有1个字节长度,因此不支持多个字符
我创建了一个扩展KeyEvent的类:
public class myKeyEvent extends KeyEvent {
public static final int MY_KEYCODE_01 = KeyEvent.KEYCODE_A;
//...
public static final int MY_KEYCODE_30 = KeyEvent.KEYCODE_Z;
}
Run Code Online (Sandbox Code Playgroud)
现在,我想从另一个类(另一个文件)获取变量名称的Integer值(例如"MY_KEYCODE_01"应该返回整数值KeyEvent.KeyCODE_A).
我试过了:
try{
Class cls = myKeyEvent.class.getClass();
Field field = cls.getDeclaredField("MY_KEYCODE_01");
int value = (Integer) field.get(cls);
Log.v("TAG", "Field value is " + value);
} catch (NoSuchFieldException e) {
Log.e("TAG", "Field either doesn't exist or is not public: " + e.toString() );
}
Run Code Online (Sandbox Code Playgroud)
在LogCat中:
Field either doesn't exist or is not public: java.lang.NoSuchFieldException: MY_KEYCODE_01
Run Code Online (Sandbox Code Playgroud)
我该怎么做?
在找到第一个数字之前,如何找到第一个子字符串?
例子:
my $string = 'AAAA_BBBB_12_13_14' ;
Run Code Online (Sandbox Code Playgroud)
预期结果:'AAAA_BBBB_'