Got Questions? Get Answers.
Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
regexp with parentheses

Subject: regexp with parentheses

From: spasmous

Date: 26 Feb, 2009 17:25:06

Message: 1 of 3

A weird problem when there are parentheses inside the pattern to be
searched.

str = '(0018,0011) DS 128 # 1, 4 Columns'

patt = '(0018,0011) DS ';
regexp(str,patt)
ans = []

% take off leading parenthesis
patt = '0018,0011) DS ';
regexp(str,'0018,0011)\sDS')
ans = 2

The first search fails when the pattern has both '(' and ')' enclosing
part of the string. Is this a bug?

Subject: regexp with parentheses

From: spasmous2

Date: 26 Feb, 2009 17:55:59

Message: 2 of 3

On Feb 26, 9:25=A0am, spasmous <spasm...@gmail.com> wrote:
> A weird problem when there areparenthesesinside the pattern to be
> searched.
>
> str =3D '(0018,0011) DS 128 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0# 1, 4=
 Columns'
>
> patt =3D '(0018,0011) DS ';regexp(str,patt)
> ans =3D []
>
> % take off leading parenthesis
> patt =3D '0018,0011) DS ';regexp(str,'0018,0011)\sDS')
> ans =3D 2
>
> The first search fails when the pattern has both '(' and ')' enclosing
> part of the string. Is this a bug?


There's also an issue with a trailing space:

regexp(str,'(0018,0011)')
ans =3D 2

regexp(str,'(0018,0011) ')
ans =3D []

Subject: regexp with parentheses

From: Ashish Uthama

Date: 26 Feb, 2009 18:20:27

Message: 3 of 3

On Thu, 26 Feb 2009 12:25:06 -0500, spasmous <spasmous@gmail.com> wrote:

> A weird problem when there are parentheses inside the pattern to be
> searched.
>
> str = '(0018,0011) DS 128 # 1, 4 Columns'
>
> patt = '(0018,0011) DS ';
> regexp(str,patt)
> ans = []
>
> % take off leading parenthesis
> patt = '0018,0011) DS ';
> regexp(str,'0018,0011)\sDS')
> ans = 2
>
> The first search fails when the pattern has both '(' and ')' enclosing
> part of the string. Is this a bug?

'(' has specical significance when it appears with a ')' in the pattern,
hence needs to be escaped


>> regexp('((','(')
ans =
      1 2
>> regexp('((','\(')
ans =
     1 2
>> regexp('()()','()')
ans =
      []
>> regexp('()()','\(\)')
ans =
      1 3
 

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us