2

Part of my project is to retrieve a string variable from an external source (google docs) and parse it. This string represents width and height. I have no problem retrieving, I just need to parse it in to two strings. The string has 4 variations. Here are examples:

3"x4"
3"hx4"w
3hx4w
3x4

The width is always the first number and the height is always the second. Sometimes, the width and height have decimal points.

I'm a regex nube. If anyone can help me parse this into two strings of the numeric values only, I would be much obliged.

3 Answers 3

3
Dim matches = Regex.Matches("3.45x4.3""", "[\d.]+")

Console.WriteLine("width: " + matches(0).Value)
Console.WriteLine("height: " + matches(1).Value)

An English representation of the regex is basically [\d.] is a character that is either a digit or a dot. The + means one or more.

Sign up to request clarification or add additional context in comments.

1 Comment

For the vb folk, here's the code in vb: Dim matches = Regex.Matches("3.45x4.3""", "[\d.]+") Console.WriteLine("width: " + matches(0).Value) Console.WriteLine("height: " + matches(1).Value)
2

I recommend you learn to fish as it will pay off dividends, and this is an extremely simple string to parse with regex.

Edit: It is even simpler than I thought. You don't have to use most of the stuff I mentioned here, as you won't have to wade through multiple of these measurements on the same line. Check out Yuriy's answer. Still, check out the rest of this and start learning some regex :)

You'll have to use grouping/captures to grab the data out that you've matched. Wrap what you are trying to match with parens to do this:

(someTextToMatch)

Don't group things you don't need to capture, or use non-capturing groups for those:

(?:someTextToMatch)

(you probably won't need these for this example, but you may need them eventually as you only get 9 captures)

Immediately useful language elements:

\s     match any single whitespace character
\d     match any single digit
.      match any single character
[Xx]   match a single upper-case or lower-case x
?      match one or zero of the previous match
+      match one or more of the previous match
*      match zero or more of the previous match (probably won't need this here)

Some docs:

I also recommend googling for regular expression tutorials. Here's one that is .Net specific:

Comments

1

Somthing like the following would be the quick way of doing it

Dim s as string = GetStrinFromDocs()

' remove quotes
s.replace("""","")

'remove other chars
s.replace("w","")
s.replace("h","")

dim Width as integer = ctype(s.split("x")(0),integer)
dim Height as integer = ctype(s.split("x")(1),integer)

You should consider using regular expressions to do this too,

http://www.regular-expressions.info/dotnet.html

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.