|
-
Feb 2nd, 2004, 05:23 PM
#1
Thread Starter
Addicted Member
representing a string with Regular Expressions (resolved)
As part of an assignment I am trying to represent a string with RE which stands for regular expressions. The string is delimited by double quotes. It can have newline characters at the end. I was wondering if anyone might give me some feedback on if its correct and how to handle the newline characters.
I have this so far:
String Literals with RE
( " ) ( 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 ) | ( a | b | c | d | e | f | g | h | i | j | k | l | m | n | o | p | q | r | s | t | u | v | w | x | y | z ) ( 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 )*
( a | b | c | d | e | f | g | h | i | j | k | l | m | n | o | p | q | r | s | t | u | v | w | x | y | z )* (newlines will go here) (")
Anyone know how I can put in newlines and if the rest is correct?
Last edited by abcdefg; Feb 3rd, 2004 at 02:03 PM.
-
Feb 2nd, 2004, 05:45 PM
#2
Well the regular expression you have just posted would match a string like this:
"f48494990434668646758ndrk\r\n"
\r\n = New Line
The regular expression you wrote is similar to saying. Match a string whose first character is a double quotation, the second is a numerical character between 0 and 9 or a lower case letter followed by zero or more numerical characters between 0 and 9 followed by zero or more lower case characters. The string must end in with a double quotation and a newline.
Rather than writing all the letters and numbers you could do this:
/ "([0-9] | [a-z])[0-9]*[a-z]*"$ /
The $ symbol mathes the end of line character. Where as [a-z] is a character class representing a range of characters.
-
Feb 2nd, 2004, 05:54 PM
#3
Thread Starter
Addicted Member
My regular expression is supposed to represent any string and the string is delimited by ". It can end with a newline character. Its right I think my question is How do I represent a newline character at the end of the string? I cant use [] $ or other extended RE notation.
-
Feb 3rd, 2004, 02:51 AM
#4
Originally posted by abcdefg
My regular expression is supposed to represent any string and the string is delimited by ". It can end with a newline character. Its right I think my question is How do I represent a newline character at the end of the string? I cant use [] $ or other extended RE notation.
It depends on what characters your string can contain. If it is just letters and numbers then a simple:
Code:
((( 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 ) | ( a | b | c | d | e | f | g | h | i | j | k | l | m | n | o | p | q | r | s | t | u | v | w | x | y | z ) | (A | B | C | D | E | F | G | H | I | J | K | L | M | N | O | P | Q | R | S | T | U | V | W | X | Y | Z))*")+(\n | (\r\n))
The RE above will match an number of alpha-numeric strings delimited by quotes. \n matches a UNIX style newline and \r\n matches a Wdinwos style newline.
If you want to match a string which includes any ascii character It would be a little more complicated.
-
Feb 3rd, 2004, 02:03 PM
#5
Thread Starter
Addicted Member
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|