正则表达式删除所有仅包含1个单词的行

时间:2018-10-04 04:25:14

标签: regex sed

我正在尝试创建一个正则表达式,该表达式将找到在其上仅包含一个单词的任何行。用连字符或符号(例如测试词)或前导空格分隔的单词仍应视为一个单词。

$cat file1
this line has many words
hello
  test-hi
this does aswell

使用正则表达式

'/^\s*(\w+)\s$/GM'

仅返回“ hello”,而忽略“ test-hi”

我能够捕获所有单个单词,但不能捕获带有连字符等的单词!

3 个答案:

答案 0 :(得分:4)

使用if(isset($_POST['sub'])) { $name = $_POST['name']; $requestXML = '<TALLYMESSAGE xmlns:UDF="TallyUDF"> <VOUCHER REMOTEID="123" VCHTYPE="Receipt" VCHKEY="321" ACTION="Create" OBJVIEW="Accounting Voucher View"> <OLDAUDITENTRIES.LIST> </OLDAUDITENTRIES.LIST> <ACCOUNTAUDITENTRIES.LIST> </ACCOUNTAUDITENTRIES.LIST> <AUDITENTRIES.LIST> </AUDITENTRIES.LIST> <OLDAUDITENTRYIDS.LIST TYPE="Number"> <OLDAUDITENTRYIDS>-1</OLDAUDITENTRYIDS> </OLDAUDITENTRYIDS.LIST> <DATE>20180801</DATE> <GUID></GUID> <VOUCHERTYPENAME>Receipt</VOUCHERTYPENAME> <VOUCHERNUMBER>5</VOUCHERNUMBER> <PARTYLEDGERNAME>'.$name.'</PARTYLEDGERNAME> <CSTFORMISSUETYPE/> <CSTFORMRECVTYPE/> <FBTPAYMENTTYPE>Default</FBTPAYMENTTYPE> <PERSISTEDVIEW>Accounting Voucher View</PERSISTEDVIEW> <VCHGSTCLASS/> <DIFFACTUALQTY>No</DIFFACTUALQTY> <AUDITED>No</AUDITED> <FORJOBCOSTING>No</FORJOBCOSTING> <ISOPTIONAL>No</ISOPTIONAL> <EFFECTIVEDATE>20180801</EFFECTIVEDATE> <ISFORJOBWORKIN>No</ISFORJOBWORKIN> <ALLOWCONSUMPTION>No</ALLOWCONSUMPTION> <USEFORINTEREST>No</USEFORINTEREST> <USEFORGAINLOSS>No</USEFORGAINLOSS> <USEFORGODOWNTRANSFER>No</USEFORGODOWNTRANSFER> <USEFORCOMPOUND>No</USEFORCOMPOUND> <ALTERID>20</ALTERID> <EXCISEOPENING>No</EXCISEOPENING> <USEFORFINALPRODUCTION>No</USEFORFINALPRODUCTION> <ISCANCELLED>No</ISCANCELLED> <HASCASHFLOW>Yes</HASCASHFLOW> <ISPOSTDATED>No</ISPOSTDATED> <USETRACKINGNUMBER>No</USETRACKINGNUMBER> <ISINVOICE>No</ISINVOICE> <MFGJOURNAL>No</MFGJOURNAL> <HASDISCOUNTS>No</HASDISCOUNTS> <ASPAYSLIP>No</ASPAYSLIP> <ISCOSTCENTRE>No</ISCOSTCENTRE> <ISSTXNONREALIZEDVCH>No</ISSTXNONREALIZEDVCH> <ISEXCISEMANUFACTURERON>No</ISEXCISEMANUFACTURERON> <ISBLANKCHEQUE>No</ISBLANKCHEQUE> <ISDELETED>No</ISDELETED> <ASORIGINAL>No</ASORIGINAL> <VCHISFROMSYNC>No</VCHISFROMSYNC> <MASTERID>9</MASTERID> <VOUCHERKEY></VOUCHERKEY> <INVOICEDELNOTES.LIST> </INVOICEDELNOTES.LIST> <INVOICEORDERLIST.LIST> </INVOICEORDERLIST.LIST> <INVOICEINDENTLIST.LIST> </INVOICEINDENTLIST.LIST> <ATTENDANCEENTRIES.LIST> </ATTENDANCEENTRIES.LIST> <ORIGINVOICEDETAILS.LIST> </ORIGINVOICEDETAILS.LIST> <INVOICEEXPORTLIST.LIST> </INVOICEEXPORTLIST.LIST> <ALLLEDGERENTRIES.LIST> <AMOUNT>200000.00</AMOUNT> <OLDAUDITENTRYIDS.LIST TYPE="Number"> <OLDAUDITENTRYIDS>-1</OLDAUDITENTRYIDS> </OLDAUDITENTRYIDS.LIST> <LEDGERNAME>'.$name.'</LEDGERNAME> <GSTCLASS/> <ISDEEMEDPOSITIVE>No</ISDEEMEDPOSITIVE> <LEDGERFROMITEM>No</LEDGERFROMITEM> <REMOVEZEROENTRIES>No</REMOVEZEROENTRIES> <ISPARTYLEDGER>Yes</ISPARTYLEDGER> <ISLASTDEEMEDPOSITIVE>No</ISLASTDEEMEDPOSITIVE> <BANKALLOCATIONS.LIST></BANKALLOCATIONS.LIST> <BILLALLOCATIONS.LIST></BILLALLOCATIONS.LIST> <INTERESTCOLLECTION.LIST></INTERESTCOLLECTION.LIST> <OLDAUDITENTRIES.LIST></OLDAUDITENTRIES.LIST> <ACCOUNTAUDITENTRIES.LIST></ACCOUNTAUDITENTRIES.LIST> <AUDITENTRIES.LIST></AUDITENTRIES.LIST> <TAXBILLALLOCATIONS.LIST></TAXBILLALLOCATIONS.LIST> <TAXOBJECTALLOCATIONS.LIST></TAXOBJECTALLOCATIONS.LIST> <TDSEXPENSEALLOCATIONS.LIST></TDSEXPENSEALLOCATIONS.LIST> <VATSTATUTORYDETAILS.LIST></VATSTATUTORYDETAILS.LIST> <COSTTRACKALLOCATIONS.LIST></COSTTRACKALLOCATIONS.LIST> </ALLLEDGERENTRIES.LIST> <ALLLEDGERENTRIES.LIST> <OLDAUDITENTRYIDS.LIST TYPE="Number"> <OLDAUDITENTRYIDS>-1</OLDAUDITENTRYIDS> </OLDAUDITENTRYIDS.LIST> <LEDGERNAME>Bank of Maharashtra</LEDGERNAME> <GSTCLASS/> <ISDEEMEDPOSITIVE>Yes</ISDEEMEDPOSITIVE> <LEDGERFROMITEM>No</LEDGERFROMITEM> <REMOVEZEROENTRIES>No</REMOVEZEROENTRIES> <ISPARTYLEDGER>Yes</ISPARTYLEDGER> <ISLASTDEEMEDPOSITIVE>Yes</ISLASTDEEMEDPOSITIVE> <AMOUNT>-200000.00</AMOUNT> <BANKALLOCATIONS.LIST> <DATE>20180801</DATE> <INSTRUMENTDATE>20180801</INSTRUMENTDATE> <BANKBRANCHNAME>Warje</BANKBRANCHNAME> <TRANSACTIONTYPE>Cheque/DD</TRANSACTIONTYPE> <BANKNAME>AXIS</BANKNAME> <PAYMENTFAVOURING>'.$name.'</PAYMENTFAVOURING> <INSTRUMENTNUMBER>50026</INSTRUMENTNUMBER> <UNIQUEREFERENCENUMBER>38ROUrdV00QEKOhl</UNIQUEREFERENCENUMBER> <STATUS>No</STATUS> <CHEQUEPRINTED>-1</CHEQUEPRINTED> <AMOUNT>-200000.00</AMOUNT> </BANKALLOCATIONS.LIST> <BILLALLOCATIONS.LIST></BILLALLOCATIONS.LIST> <INTERESTCOLLECTION.LIST></INTERESTCOLLECTION.LIST> <OLDAUDITENTRIES.LIST></OLDAUDITENTRIES.LIST> <ACCOUNTAUDITENTRIES.LIST></ACCOUNTAUDITENTRIES.LIST> <AUDITENTRIES.LIST></AUDITENTRIES.LIST> <TAXBILLALLOCATIONS.LIST></TAXBILLALLOCATIONS.LIST> <TAXOBJECTALLOCATIONS.LIST></TAXOBJECTALLOCATIONS.LIST> <TDSEXPENSEALLOCATIONS.LIST></TDSEXPENSEALLOCATIONS.LIST> <VATSTATUTORYDETAILS.LIST> </VATSTATUTORYDETAILS.LIST> <COSTTRACKALLOCATIONS.LIST> </COSTTRACKALLOCATIONS.LIST> </ALLLEDGERENTRIES.LIST> <ATTDRECORDS.LIST> </ATTDRECORDS.LIST> </VOUCHER> </TALLYMESSAGE>'; $server = 'http://server:9999'; $headers = array( "Content-type: text/xml" ,"Content-length: ".strlen($requestXML) ,"Connection: close" ); $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $server); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_TIMEOUT, 100); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_POSTFIELDS, $requestXML); curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); $data = curl_exec($ch); if(curl_errno($ch)) { print curl_error($ch); echo " something went wrong..... try later"; } else { echo " request accepted"; print $data; curl_close($ch); } }更容易,默认情况下,它将根据一个或多个连续空格将每条记录分成多个字段,并且行首/尾的空格将不属于字段计算

awk

$ awk 'NF==1' ip.txt hello test-hi $ awk 'NF>1' ip.txt this line has many words this does aswell 是一个内置变量,指示输入记录中的字段数

答案 1 :(得分:1)

您可以使用

^\s*([\w-]+)\s*$

增加了对连字符的支持,使第二个\s匹配“零个或多个”空格。保留您的GM标志。

Demo

答案 2 :(得分:1)

尝试使用\S来匹配任何非空格字符:

'/^\s*(\S+)\s$/GM'