Jump to content

What is regexp for -?

- - - - -

  • Please log in to reply
4 replies to this topic

#1
onething

onething

    Programmer

  • Members
  • PipPipPipPip
  • 118 posts
I get that [A-Z,a-z,0-9] is all upper, lower case letters and all numbers. But what to do with all the other keys on the keyboard? Right now I want the - sign, but who knows what I'll be needing another day.

Is it d+, or $, or *? Problem with these three is they seem to be too broad. Surely there's a smaller more specific option, no?

What would you do to cover this kind:

/F2BAC-HE35G-130075

[A-Z,a-z,0-9] doesnt get the first - and so stops and gives up.

#2
cdg10620

cdg10620

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 389 posts
You could so something like [a-zA-Z0-9]{5}-[a-zA-Z0-9]{5}-[a-zA-Z0-9]{5}

This will validate that string and allow any case number or any number and it expects three sets of 5 characters separated by a hyphen. Let me know if that helps. I think the commas in your example were breaking the regex.
-CDG10620
Software Developer

#3
onething

onething

    Programmer

  • Members
  • PipPipPipPip
  • 118 posts
thanks, but the commas worked fine.

my solution works slightly better than yours :c-whistle: cuz it's smaller.

but it still isn't good enough because i don't know a generic regexp that will cover all things like !"·$%&/(/)=?¿`+`'¡ç´Ç¨´;.:,-_

and i have to use the literal '-' to cover the real '-'

so i did:

[a-z,A-Z,0-9,-]*

#4
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,118 posts
  • Location:Vancouver, Eh! Cleverness: 200
You are very unclear on what you wish to match,

1) Ranges are delimited by values not commas, repeating them is a recoverable syntax error.
2) If you are wishing to capture all non-"words" (letters or numbers) in a string, then the character will be the following: \W
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#5
cdg10620

cdg10620

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 389 posts
*shrug* I've never claimed to be a regex guru. Glad you found something that works for you.
-CDG10620
Software Developer




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users