我正在使用正则表达式检查字符串.
规则是:
字符串可以,
包含任何数字,连字符和逗号
连字符和逗号应该只在数字之间.它不应该在字符串的开头或结尾.
逗号是可选的.连字符是强制性的
例如,
有效期:
10-20
10-20-3
10-20,3
Run Code Online (Sandbox Code Playgroud)
InValid:
10
-10
,10
10-20,
10-20-
10,20
Run Code Online (Sandbox Code Playgroud)
我到目前为止尝试的代码:
[0-9,-]+
Run Code Online (Sandbox Code Playgroud)
有人可以建议如何检查昏迷和连字符不应该在字符串的开头或结尾以及上述条件?
我正在为文本文件进行自定义验证.这是第一次对整个文件进行验证.如果有错误,则显示在另一个JTextArea中.之后,仅对那些可见的部分进行验证.我的问题是我无法存储以前的错误并结合当前显示的错误.例如,第一次,我在第1行,第5行,第6行,第100行,第500行得到错误.从下次可见部分将是1-50行,验证将仅针对1-50进行,因此100和500的错误消失了.我通过将先前的错误存储在临时变量中来尝试它,接下来的问题是,现在私有错误将只变为1-50,有人可以告诉我如何去做吗?
我试过的代码,
private void displayError() {
errorText.setText(null);
String err = null;
try{
//tempErrors
if(!prevErrors.isEmpty()){
for(int i=0; i< prevErrors.size(); i++){
err = prevErrors.get(i).toString();
if(!errors.contains(err)){
errors.add(prevErrors.get(i).toString());
}
}
}
}
catch(Exception ex){
System.out.println(ex);
}
Iterator it = errors.iterator();
while(it.hasNext()){
errorText.append(it.next() + "\n");
}
prevErrors = new ArrayList();
for(int i=0; i< errors.size(); i++){
prevErrors.add(errors.get(i).toString());
}
}
Run Code Online (Sandbox Code Playgroud) 我有一个文件,我将逐行阅读.通过使用split方法将每一行拆分为单词,并根据单词的位置(每行的前4个字符等)对单词进行着色,也可以基于单词.不同的颜色应该应用于不同的单词,如下所示.我想知道哪个班级有用,我看了一下荧光笔.任何建议,举例都会非常有帮助
String text = textArea.getText();
String newLine = "\n";
String spaceDelim = "[ ]+";
String[] tokens;
String lines = text.split(newLine);
for(String line : lines) {
tokens = line.split(spaceDelim);
tokens[1] //should be in redColor
tokens[2] //should be in greenColor
tokens[3] tokens[4] //should in blueColor
}
Run Code Online (Sandbox Code Playgroud) 我正在使用swing worker来验证文件,我希望将startLine和endLine发送到validate类,因为我不想每次都验证整个文件.第一次当现有文件是openend时,我想将startLine发送为0,将endLine作为endLine = editorTextArea.getLineCount() - 1;发送.之后我应该能够每秒钟将startLine和endLine发送给我.我如何实现这一目标?
验证课程:
class Validate implements Runnable {
private JTextArea editorTextArea;
private JTextArea errorTextArea;
private int startLine;
private int endLine;
public Validate(JTextArea editor, JTextArea error, startLine, endLine) {
this.editorTextArea = editor;
this.errorTextArea = error;
this.startLine = startLine;
this.endLine = endLine;
}
@Override
public void run() {
SwingWorker worker = new SwingWorker<Void, Void>() {
@Override
protected Void doInBackground() throws Exception {
//CODE TO VALIDATE
return null;
}
@Override
protected void done() {
//CODE TO DISPLAY THE RESULT …Run Code Online (Sandbox Code Playgroud) 我试图匹配某些行的完整单词,想知道如何在正则表达式中使用OR,如果我只使用一个关键字,它工作正常.例,
regex = ".*\\b" + "KEYWORD1" + "\\b.*";
String regex = ".*\\b" + "KEYWORD1|KEYWORD2|KEYWORD3" + "\\b.*";
for (int i = start; i < end; i++) {
if (lines[i].matches(regex)) {
System.out.println("Matches");
}
}
Run Code Online (Sandbox Code Playgroud) 我想知道如果存在条件,是否有任何替代或优雅的写作方式
是必须检查的更多条件.
例,
if(cond1){
if(cond2){
if(cond3){
if(cond4){
//all conditions are checked
flag = true;
}
}
}
}
Run Code Online (Sandbox Code Playgroud) 我正在使用php删除包含已删除帖子的图像的文件夹.我正在使用下面的代码,我在网上找到并做得很好.
我想知道如果其中还有其他文件夹,我怎么才能删除文件夹中的特定文件夹.
当我使用下面的代码时,怎么可能这样做?使用:/ dev/images/norman/8 - >不会删除文件夹8使用:/ dev/images/norman/ - >将删除所有文件夹
Eg:
/dev/images/norman/8 -> I need to delete only this folder
/dev/images/norman/9
/dev/images/norman/10
/dev/images/norman/11
/dev/images/norman/12
<?php
$path = $_SERVER['DOCUMENT_ROOT'].'/dev/images/norman/8';
emptyDir($path);
function emptyDir($path) {
// INITIALIZE THE DEBUG STRING
$debugStr = '';
$debugStr .= "Deleting Contents Of: $path<br /><br />";
// PARSE THE FOLDER
if ($handle = opendir($path)) {
while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != "..") {
// IF IT"S A FILE THEN DELETE IT …Run Code Online (Sandbox Code Playgroud) public static void main(String[] args) {
String[] errorStr = new String[] {
"Line No: " + " " + 1,
"Line No: " + " " + 11,
"Line No: " + " " + 10,
"Line No: " + " " + 2,
"Line No: " + " " + 3
};
Arrays.sort(errorStr);
for(Object obj : errorStr){
System.out.println(obj);
}
}
Run Code Online (Sandbox Code Playgroud)
有人可以指出为什么排序在这里不起作用?
expected is,
Line No: 1
Line No: 2
Line No: 3
Line No: 10
Line No: 11
Actual …Run Code Online (Sandbox Code Playgroud) 我有一行可以匹配多个关键字.整个关键字应该匹配.
例,
String str = "This is an example text for matching countries like Australia India England";
if(str.contains("Australia") ||
str.contains("India") ||
str.contains("England")){
System.out.println("Matches");
}else{
System.out.println("Does not match");
}
Run Code Online (Sandbox Code Playgroud)
这段代码工作正常.但如果要匹配的关键字过多,则该行会增长. 有没有优雅的方式编写相同的代码? 谢谢
我是CodeIgniter的新手.在模型中,我有以下代码:
public function get_all_subjects()
{
return $this->db->get('subjects');
}
Run Code Online (Sandbox Code Playgroud)
在控制器中,我有:
public function index()
{
$this->load->model('subjects');
$data['content'] = $this->subjects->get_all_subjects();
$this->load->view('home', $data);
}
Run Code Online (Sandbox Code Playgroud)
我试图在视图中获取值:
foreach($content as $val)
{
echo $val['subject']; //i am getting error, Message: Undefined index: subject
}
Run Code Online (Sandbox Code Playgroud)
subjects表中的字段是subject_id和subject.
我收到此错误消息:
未定义的索引:主题
java ×8
regex ×3
swing ×3
arrays ×2
php ×2
codeigniter ×1
coding-style ×1
colors ×1
directory ×1
exception ×1
fonts ×1
jtextarea ×1
recursion ×1
sorting ×1
swingworker ×1