在Perl中仅打印匹配的字符

时间:2019-09-19 20:38:58

标签: regex perl

如果我只想使用Perl打印匹配的字符,是否有Perl一线纸。例如,我有一个像这样的文本文件:

let students = [{ batch_id: 22, id: 1, image: null, name: "a new batch student", attendance: [{ id: 1, student_id: 1, batch_id: 22, absent_on: "2019-09-15", time: "09:26:23" }, { id: 9, student_id: 1, batch_id: 22, absent_on: "2019-09-19", time: "00:00:00" }] }, { batch_id: 22, id: 2, image: null, name: "a new batch student", attendance: [{ id: 9, student_id: 2, batch_id: 22, absent_on: "2019-09-19", time: "00:00:00" }] }, { batch_id: 22, id: 12, image: null, name: "a new batch student", attendance: [] }];

function searchAbsentDate( students, date ) {
   
    const 
        isDateFormat = datestring => /\d{4}-\d{2}-\d{2}/.test( datestring ),
        isNotString = s => !typeof s === "string",
        isNotDate = potentialDate => ( isNotString( potentialDate ) || !isDateFormat ),
    
        noAttendanceRecords = student => !student.attendance || student.attendance.length === 0,
        invalidStudentAttendanceRecords = attendance => !Array.isArray( attendance ),
    
        noAbsentDate = absentDate => !absentDate,
    
        forAllAttendanceRecords = ( attendanceRecords, index ) => checkFn => attendanceRecords.some( checkFn( index ) ),
        filterRecords = students => filterFn => students.filter( filterFn ),
    
        ifAbsentOnDate = date => index => ( record, _index ) => {
            if ( noAbsentDate( record.absent_on ) ) err.throwMissingDate( index, _index );
            return record.absent_on == date;
        },
        forAbsenceOnDate = ( student, studentIndex ) => {
            if ( noAttendanceRecords( student ) ) return false;
            if ( invalidStudentAttendanceRecords( student.attendance ) ) err.throwInvalidStudentRecord();
            return forAllAttendanceRecords( student.attendance, studentIndex )( ifAbsentOnDate( date ) );
        },
    
    err = {
        throwMissingDate( stu_i, ar_i ) {
            throw Error( "Invalid Attendance Record Format At Student:" + stu_i + "Attendance Record:" + ar_i );
        },
        throwInvalidStudentRecord( index ) {
            throw Error( "Invalid Student Record Format At " + index );
        },
        throwStudentRecordsFormat() {
            throw Error( "Student Records is not Array" );
        },
        throwDateFormat() {
            throw Error( "Improper Date Format" );
        }
    };
   
    if ( isNotDate( date ) ) err.throwDateFormat();
    if ( !Array.isArray( students ) ) err.throwStudentRecordsFormat();
    return filterRecords( students )( forAbsenceOnDate );
}

console.log( searchAbsentDate( students, "2019-09-19" ) );

我尝试了以下操作,但没有输出:

data.txt
foo1 bar baz
foo2 bar baz
bar foo3 baz
bar baz foo4

1 个答案:

答案 0 :(得分:4)

#tabletu tr:nth-of-type(2) td {padding: 0;} 包含与第一个捕获组匹配的部分,但是您的正则表达式中没有捕获组。

使用$1可以使整个模式匹配所有内容。

$&