我有这行文字,引号的数量可能会改变如下:
Here just one "comillas"
But I also could have more "mas" values in "comillas" and that "is" the "trick"
I was thinking in a method that return "a" list of "words" that "are" between "comillas"
我如何获得结果应该是引号之间的数据?:
科米利亚斯
mas,comillas,trick
a,words,are,comillas
答案 0 :(得分:46)
您可以使用正则表达式来捕获此类信息。
Pattern p = Pattern.compile("\"([^\"]*)\"");
Matcher m = p.matcher(line);
while (m.find()) {
System.out.println(m.group(1));
}
此示例假定要解析的行的语言不支持字符串文字中双引号的转义序列,包含跨越多个“行”的字符串,或支持其他字符串(如单引号)的分隔符。 / p>
答案 1 :(得分:15)
查看Apache commons-lang库中的StringUtils
- 它有一个substringsBetween
方法。
String lineOfText = "if(getip(document.referrer)==\"www.eg.com\" || getip(document.referrer)==\"192.57.42.11\"";
String[] valuesInQuotes = StringUtils.substringsBetween(lineOfText , "\"", "\"");
assertThat(valuesInQuotes[0], is("www.eg.com"));
assertThat(valuesInQuotes[1], is("192.57.42.11"));
答案 2 :(得分:0)
String line = "if(getip(document.referrer)==\"www.eg.com\" || getip(document.referrer)==\"192.57.42.11\"";
StringTokenizer stk = new StringTokenizer(line, "\"");
stk.nextToken();
String egStr = stk.nextToken();
stk.nextToken();
String ipStr = stk.nextToken();
答案 3 :(得分:0)
首先,请注意您应该使用equals()而不是==。 “==”默认情况下会询问它们是否是内存中的相同实例,在字符串中有时可能就是这种情况。使用myString.equals(“...”),您将比较字符串的值。
至于如何获得引号之间的值我不确定你的意思。 “......”是一个实际的对象。或者你可以这样做:
String webUrl =“www.eg.com”;
答案 4 :(得分:0)
如果要解析整个源文件而不是一行,那么基于函数语法的解析器可能比尝试基于字符串执行此操作更安全。
我猜这些将是你语法中的字符串文字。
答案 5 :(得分:0)
如果您想从文件中获取::
public class ChoiceGame extends AppCompatActivity {
TextView modeText;
SeekBar seekBarMode;
Button playGame, scoreGame;
WorldCountryDatabase worldCountryDatabase;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game_flag);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
if (getSupportActionBar() != null)
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
modeText = (TextView) findViewById(R.id.modeText);
seekBarMode = (SeekBar) findViewById(R.id.seekBarMode);
playGame = (Button) findViewById(R.id.playGame);
scoreGame = (Button) findViewById(R.id.scoreGame);
worldCountryDatabase = new WorldCountryDatabase(this);
try {
worldCountryDatabase.createDatabase();
} catch (Exception e) {
e.printStackTrace();
}
//Event
seekBarMode.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
if (progress == 0)
modeText.setText(Common.MODE.EASY.toString());
else if (progress == 1)
modeText.setText(Common.MODE.MEDIUM.toString());
else if (progress == 2)
modeText.setText(Common.MODE.HARD.toString());
else if (progress == 3)
modeText.setText(Common.MODE.HARDEST.toString());
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
playGame.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(getApplicationContext(), PlayGameCountry.class);
intent.putExtra("Mode", getPlayMode()); // Send Mode to Playing page
startActivity(intent);
finish();
}
});
scoreGame.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(getApplicationContext(), ScoreGame.class);
startActivity(intent);
finish();
}
});
}
private String getPlayMode() {
if (seekBarMode.getProgress() == 0)
return Common.MODE.EASY.toString();
else if (seekBarMode.getProgress() == 1)
return Common.MODE.MEDIUM.toString();
else if (seekBarMode.getProgress() == 2)
return Common.MODE.HARD.toString();
else
return Common.MODE.HARDEST.toString();
}