0

My question is how to match the first three characters of certain lines within a string using regular expressions the regex i have should work however when i run the program it only matches the first three characters of the first line the string is

.V/RTEE/EW\n.N/ERER/JAN/21

my regex is ^(.[VN]/)* so it needs to match .V/ and .N/ any help I will be very grateful

CodersSC
  • 710
  • 2
  • 13
  • 29

1 Answers1

1

You need to suppress the special meaning of the . and /
Use \ in-front of them.

Naveen
  • 124
  • 1
  • 14
  • Try this instead ^(\.[VN]\/)(.*) – Naveen Dec 09 '11 at 17:04
  • hmm i thin the '.' whill catch the whole line though won't it ? – CodersSC Dec 09 '11 at 17:09
  • . will match any single character except space and * will catch 0 or any number of characters. If you still think only (.*) matches everything you can use $ which matches end of the string – Naveen Dec 09 '11 at 17:15
  • ok i will try it when i log back onto the PC so this regeular expression should catch .V/ and .N/ or any three characters at the start of the line i.e .S/ if .S/EE/ER/GEE was there too and the regular expression had [VS] instead of [VN] ? (just to clarify) – CodersSC Dec 09 '11 at 17:19
  • if you want to catch any of three use [VSN] – Naveen Dec 09 '11 at 17:24
  • this expression does not work it catch the whole line instead of .V/ .N/ .S/ – CodersSC Dec 10 '11 at 12:34
  • Which RE are you using, it is working perfectly for me. It is ^(\.[VNS]\/)(.*)$ – Naveen Dec 12 '11 at 14:21