Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

pattern matching

kris007kris007 Member Posts: 1
hi
I would like to
find the exact number of occurences of a - , (2 hypens) -- ,---(3 hypens)
in my input string

--sssssdf-dfdf--dfdf---lkllll

how do I achieve it
if i just use
/[-]{1}/ for searchin strictly one occurence and
/[-]{2}/ for 2 occurrences etc
My match also considers each of the '-'(hypens) in the string -- or ---
as a single occurence
Thanks
kris

Comments

  • StefanoStefano Member Posts: 18
    there are 3 ways (at least, no guru here).

    1. /[-]{n, m}/ -> that means "-" must be present at least n times, max m times, so if you place this in your code:

    /[-]{1, 1}/,

    all should go as you planned.

    2. You just have to specify, in your program, that before and after "-" there can be anything, even a null, but another "-":

    /[!-][-][!-]/

    something like this should work it out (don't know, I ain't a Perl guru)

    3. just put it in a variable, then look for "---", paying attention to substitute it with "" (you should begin to understand now =D)
    nex time you do it with "--", and then with "-". You can also automatize it making it look for /[-]{1}/, getting the longer string (e.g. "----"), putting in a variable and then cycling it out until it becomes "-".
    I just hope to have been of some aid.
    For anything else just ask me.
    Ciao,
    Stefano.

    : hi
    : I would like to
    : find the exact number of occurences of a - , (2 hypens) -- ,---(3 hypens)
    : in my input string
    :
    : --sssssdf-dfdf--dfdf---lkllll
    :
    : how do I achieve it
    : if i just use
    : /[-]{1}/ for searchin strictly one occurence and
    : /[-]{2}/ for 2 occurrences etc
    : My match also considers each of the '-'(hypens) in the string -- or ---
    : as a single occurence
    : Thanks
    : kris
    :

  • Andre YoungAndre Young USAMember Posts: 0

    _____ [ http://forcoder.org ] free ebooks and video tutorials about | PHP, Objective-C, Visual Basic, Delphi, Scratch, C++, C, Python, Perl, Swift, R, PL/SQL, Java, JavaScript, Visual Basic .NET, Assembly, MATLAB, Go, Ruby, C# Lisp, Crystal, Hack, COBOL, Fortran, ABAP, Scheme, Erlang, Rust, Bash, Scala, VBScript, Apex, ML, Kotlin, Awk, D, F#, SAS, Clojure, Ada, Alice, LabVIEW, Dart, Logo, Prolog, Transact-SQL, FoxPro, Lua, Julia | ______

Sign In or Register to comment.