小编Jor*_*rge的帖子

REGEX - 匹配包含特定单词的行的第N个单词

我正在努力获得正确的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)

regex

6
推荐指数
1
解决办法
2万
查看次数

Android - 如何将KeyEvent KeyCode转换为Char?

我想在按下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个字节长度,因此支持多个字符

android

5
推荐指数
2
解决办法
2万
查看次数

如何通过名称获取"public static final int"值?

我创建了一个扩展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)

我该怎么做?

java android

3
推荐指数
1
解决办法
3733
查看次数

如何提取子串直到第一个数字?

在找到第一个数字之前,如何找到第一个子字符串?

例子:

my $string = 'AAAA_BBBB_12_13_14' ;
Run Code Online (Sandbox Code Playgroud)

预期结果:'AAAA_BBBB_'

regex perl

2
推荐指数
1
解决办法
7243
查看次数

标签 统计

android ×2

regex ×2

java ×1

perl ×1