1

In Node.js, I would like to extract multiple strings from a given string. The string to search from looks like this:

Mon May 17 10:17:44 2021 SEND MESSAGE TO NETWORK (10.0.158.24:5060 [UDP]) (BUFF LEN = 308)
----------------------utf8-----------------------
SIP/2.0 100 Trying
To: "+3212345678" <sip:[email protected];user=phone>
From: "+32456123456" <sip:[email protected];user=phone>;tag=1c506737759
Call-ID: [email protected]
CSeq: 1 INVITE
Via: SIP/2.0/UDP 10.0.158.24:5060;branch=z9hG4bKac551823835
Content-Length: 0

-------------------------------------------------
Mon May 17 10:17:44 2021 11f7 [CCall::checkAuthentication] INVITE
Mon May 17 10:17:44 2021 [DBG: CResponse::freeAttributes] Ends
Mon May 17 10:17:44 2021 6367C [CMessage::send] sip_sendMessage (10.0.158.24:5060)
Mon May 17 10:17:44 2021 SEND MESSAGE TO NETWORK (10.0.158.24:5060 [UDP]) (BUFF LEN = 496)
----------------------utf8-----------------------
SIP/2.0 480 Temporarily not available
Allow: INVITE, ACK, CANCEL, BYE, PRACK, NOTIFY, REFER, SUBSCRIBE, OPTIONS, UPDATE
User-Agent: OmniPCX Enterprise R12.4 m5.204.2.b
To: "+3212345678" <sip:[email protected];user=phone>;tag=fa4096f6eb58184a573b7fcb86a7adff
From: "+32456123456" <sip:[email protected];user=phone>;tag=1c506737759
Call-ID: [email protected]
CSeq: 1 INVITE
Via: SIP/2.0/UDP 10.0.158.24:5060;branch=z9hG4bKac551823835
Content-Length: 0

-------------------------------------------------
1621239464 -> Mon May 17 10:17:44 2021 6367C [CMessage::onSendSuccess]
Mon May 17 10:17:44 2021 2107 [CTransaction::onSendSuccess] CMessage 6367C send Success
Mon May 17 10:17:44 2021 15cd [CDialog::onSendSuccess]

I want to extract everything between '----------------------utf8-----------------------' and '-------------------------------------------------'

I tried following:

let matches = givenString.match(/-{22}utf8-{23}(.*?)-{49}/g).map((val) => {
    return val.replace(/-{22}utf8-{23}/g, '')
})
console.log(matches)

But the result is 'TypeError: Cannot read property 'map' of null'

I also tried to without counting the '-' characters

let matches = givenString.match(/----------------------utf8-----------------------(.*?)-------------------------------------------------/g)

But same result ('TypeError: Cannot read property 'map' of null')

4
  • 2
    I think you should add s flag at the end, single line. With it . matches new line characters. Commented May 18, 2021 at 13:36
  • I just tried the very same and it worked just fine. Commented May 18, 2021 at 13:39
  • Maybe the problem lies where you read the text into givenString? Commented May 18, 2021 at 13:41
  • I'm creating the String by reading a file let givenString = fs.readFileSync('test2.txt').toString(), could this be the problem ? Commented May 18, 2021 at 13:52

1 Answer 1

1

The Regexp doesn't work that way. It will not recognize the (.*?) part as to be accross multiple lines. For this you have to be using the s flag.

let matches = givenString.match(/-{22}utf8-{23}(.*?)-{49}/gs) //...
-----------------------------------------------------------^

let givenString = `Mon May 17 10:17:44 2021 SEND MESSAGE TO NETWORK (10.0.158.24:5060 [UDP]) (BUFF LEN = 308)
----------------------utf8-----------------------
SIP/2.0 100 Trying
To: "+3212345678" <sip:[email protected];user=phone>
From: "+32456123456" <sip:[email protected];user=phone>;tag=1c506737759
Call-ID: [email protected]
CSeq: 1 INVITE
Via: SIP/2.0/UDP 10.0.158.24:5060;branch=z9hG4bKac551823835
Content-Length: 0

-------------------------------------------------
Mon May 17 10:17:44 2021 11f7 [CCall::checkAuthentication] INVITE
Mon May 17 10:17:44 2021 [DBG: CResponse::freeAttributes] Ends
Mon May 17 10:17:44 2021 6367C [CMessage::send] sip_sendMessage (10.0.158.24:5060)
Mon May 17 10:17:44 2021 SEND MESSAGE TO NETWORK (10.0.158.24:5060 [UDP]) (BUFF LEN = 496)
----------------------utf8-----------------------
SIP/2.0 480 Temporarily not available
Allow: INVITE, ACK, CANCEL, BYE, PRACK, NOTIFY, REFER, SUBSCRIBE, OPTIONS, UPDATE
User-Agent: OmniPCX Enterprise R12.4 m5.204.2.b
To: "+3212345678" <sip:[email protected];user=phone>;tag=fa4096f6eb58184a573b7fcb86a7adff
From: "+32456123456" <sip:[email protected];user=phone>;tag=1c506737759
Call-ID: [email protected]
CSeq: 1 INVITE
Via: SIP/2.0/UDP 10.0.158.24:5060;branch=z9hG4bKac551823835
Content-Length: 0

-------------------------------------------------
1621239464 -> Mon May 17 10:17:44 2021 6367C [CMessage::onSendSuccess]
Mon May 17 10:17:44 2021 2107 [CTransaction::onSendSuccess] CMessage 6367C send Success
Mon May 17 10:17:44 2021 15cd [CDialog::onSendSuccess]
`

let matches = givenString.match(/-{22}utf8-{23}(.*?)-{49}/gs).map((val) => {
    return val.replace(/-{22}utf8-{23}/g, '')
})
console.log(matches)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.