extract word before and after character

i want to extract string before "(" and after " " or "," (space comma)
example:
input: pilo(52),iko(54);
result:
pilo
iko

4 Comments

Can you be more specific about what MATLAB class your input is? Here are some examples
in = ["pilo(52)","iko(54)"]
in = 1×2 string array
"pilo(52)" "iko(54)"
in = {"pilo(52)","iko(54)"}
in = 1×2 cell array
{["pilo(52)"]} {["iko(54)"]}
in = {'pilo(52)','iko(54)'}
in = 1×2 cell array
{'pilo(52)'} {'iko(54)'}
in = "pilo(52),iko(54)"
in = "pilo(52),iko(54)"
in = 'pilo(52),iko(54)'
in = 'pilo(52),iko(54)'
Is it ever possible for the input to have nested () ? For example,
pilo(sembei(93) fedora(s3))
Is it every possible for there to be digits in what is extracted? Is it ever possible for the part in () to be non-digits?
pilo17(52),iko(pippin)
hi, in " () " there is only number :
(34)
(32)
(1)
aldo
aldo on 19 Dec 2023
Edited: aldo on 20 Dec 2023
other example : (the format data is array cell)
g(1,:)={'input: pilo(52),iko(54)' }
g(2,:)={'input:iko(54)' }
g(3,:)={'input:pilo(52),iko(54)' }
g(4,:)={'input:pilo(52),iko(54),op(23)' }
in one cell there can be several words
finally I want to capture this:
in g(1,:) :
pilo 52
iko 54
in g(2,:)
iko 54
in g(3,:)
pilo 52
iko 54
in g(4,:)
pilo 52
iko 54
op 23

Sign in to comment.

 Accepted Answer

C = {'input: pilo(52),iko(54)'; 'input:iko(54)'; 'input:pilo(52),iko(54)'; 'input:pilo(52),iko(54),op(23)'}
C = 4×1 cell array
{'input: pilo(52),iko(54)' } {'input:iko(54)' } {'input:pilo(52),iko(54)' } {'input:pilo(52),iko(54),op(23)'}
X = regexp(C,'(\w+)\((\d+)','tokens');
X = cellfun(@(c)vertcat(c{:}),X,'uni',0)
X = 4×1 cell array
{2×2 cell} {1×2 cell} {2×2 cell} {3×2 cell}
X{:}
ans = 2×2 cell array
{'pilo'} {'52'} {'iko' } {'54'}
ans = 1×2 cell array
{'iko'} {'54'}
ans = 2×2 cell array
{'pilo'} {'52'} {'iko' } {'54'}
ans = 3×2 cell array
{'pilo'} {'52'} {'iko' } {'54'} {'op' } {'23'}
However... regular expressions are very precise things. Writing them well requires understanding the precise conditions that they should match. Thus usually when someone posts a question with "simplified" text it just delays getting a working solution.

2 Comments

correct! thank you
Can I ask you for further help?
i find this array cell
{'pilo'} {'52'}
{'iko' } {'54'}
{'op' } {'23'}
now I have to use these found names to capture other words:
example:
xx1(pilo,ohlcvalues) and xx1(op,ohlcvalues)=false
xx2(+iko,ohlcvalues)
i want to catch xx1 and xx2
rules: (example using 'pilo')
(pilo, xxc )
(+pilo, xvs )
(-pilo, xcs )
i want to catch the name befone '('
In this example :
xx1(pilo, .. ) ======>xx1
xx2(+pilo, .. ) ======>xx2
xx3(-pilo, .. ) ======>xx3
the new cell array created must be:
{'pilo'} {'52'} {'xx1'}

Sign in to comment.

More Answers (0)

Categories

Asked:

on 19 Dec 2023

Commented:

on 20 Dec 2023

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!