0

trying to catch the characters at the start the string and newlines the string is

.V/1LBOG\n.F/AV0094/08NOV/SAL/Y\n.E/0134249356001"

the regular expression i am using is from the string above i need to catch .V/ and .E/

^.[VE]/*

But it only seems to ctach .V/ can anyone see why as i thought ^ means newlines aswell as start of strings ? any help will be very gratefull as ive had this problem for a while now. If this is not the correct way as in doing this could you propose a different way.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
CodersSC
  • 710
  • 2
  • 13
  • 29
  • you need the "/1LBLOG" and "0134249356001" values rigth? – rkmax Dec 10 '11 at 14:01
  • no instead of that i need it to check that .V/ and .E/ are present at the start of a new line so its ".V/" not 1BLOG and ".E/" not 0134249356001 hope that helps? – CodersSC Dec 10 '11 at 14:04

3 Answers3

0

if you need .V or .E try ^.(V|E)/* the or | operator is useful for check ^.V/* or ^.E/*

rkmax
  • 17,633
  • 23
  • 91
  • 176
  • i would need it to check for both but as far as i remember i can just change that operator to the & operator or is it && – CodersSC Dec 10 '11 at 14:19
  • I recommend you try it yourself with different examples, in any text editor that supports regular expressions. I use text Sublime 2 – rkmax Dec 10 '11 at 14:25
0

Regex 101:

^ means start of string. And you guessed it right. There can only be one start of string.

^.[VE]/*

means :

Match start of string, followed by any character (other than newline), followed by either a V or a E, followed by 0 to n / (greedy).

Probably you want something like this :

\.[VE].*?(?:\\n|$)

Which means match a dot, followed by V or E and match everything until \n or end of string.

Comment if I am wrong.

So .V/1LBOG\n.F/AV0094/08NOV/SAL/Y\n.E/0134249356001"

Looks like this ?

.V/1LBOG
.F/AV0094/08NOV/SAL/Y
.E/0134249356001"

If yes, then you need to change your regex a little bit:

\.[VE].*

Abusing the fact that . does not match newlines by default.

FailedDev
  • 26,680
  • 9
  • 53
  • 73
  • that is correct but instead of eveything until a \n or end of string it would have to include \n or until the end of string because .E is on a new line ? comment if you did not understand that properly – CodersSC Dec 10 '11 at 14:15
  • yes that is what the string looks like and from that i need to check .V/ and .E/ are present but this regex ^.[VE]/ only catches .V/? – CodersSC Dec 10 '11 at 14:22
  • Yes because as I explained ^ matches the start of the string. – FailedDev Dec 10 '11 at 14:23
0

. in regular expressions matches any single character, not a literal .. If you want to match a literal period, you need to escape it (\.). * doesn't match any number of any characters (as most shells would), but instead matches zero or more instances of whatever you put before it. For example, A* will match the literal letter A, AAAA etc., and .* will match any string.

^ means the beginning of a line. ^\.[VE]/ will match .V/ and .E/ (but only at the start of the line).

Staven
  • 3,113
  • 16
  • 20
  • ok so with the regex search is that possible to also check weather .V/ and .E/ are at the start of the string or new line etc? – CodersSC Dec 10 '11 at 14:17
  • If you use regexp_match() with something like `^\.[VE]/.*$` (`$` is the end of the line), it should match only the lines that start with `.V/` or `.E/`. – Staven Dec 10 '11 at 14:20
  • it matches the .V/ but not .E/ only matches if .E/ was the start of the string – CodersSC Dec 10 '11 at 14:54