tags:

views:

313

answers:

3

Can someone show me a regex to select #OnlinePopup_AFE53E2CACBF4D8196E6360D4DDB6B70 its okay to assume #OnlinePopup

~DCTM~dctm://aicpcudev/37004e1f8000219e?DMS_OBJECT_SPEC=RELATION_ID#OnlinePopup_AFE53E2CACBF4D8196E6360D4DDB6B70_11472026_1214836152225_6455280574472127786
+2  A: 

NB: The following is .NET Regex syntax, modify for your flavour.

The following:

#[^_]+_[^_]+

will match:

  • Hash
  • One or more characters until an underscore
  • Underscore
  • One or more characters until an underscore

If the first bit is constant, and you want to be more specific you could use:

#OnlinePopup_[A-F0-9]+

This will match

  • #OnlinePopup_ (exactly)
  • One or more hex characters until a non Hex character
Robert Wagner
parsing "\#[^\_]+\_[^\_]+" - Unrecognized escape sequence \_.
joe
Sorry, try removing the \ in \_. Regex languages have little variations like this.
Robert Wagner
\#OnlinePopup_[A-F0-9]+ worked. Thanks!
joe
Were you getting the "Unrecognized escape sequence" because the compiler was trying interpreting escape sequences? I usually put an literal (@) marker at the start of regex strings: @"\#blah". Otherwise you need to double escape your \'s
Robert Wagner
The hash and underscore aren't metacharacters in regular expressions. You should not escape them. The error about \_ comes from the regex engine. .NET doesn't allow the underscore to be escaped.
Jan Goyvaerts
@Jan Interestingly it does not care about the \#. I can leave it on or off, but it throws a tantrum on \_. Post changed.
Robert Wagner
A: 

Simply matching anything between the first '#' and the first or last '_' will not work for your example since the string that you want returned has an underscore in it. If all the text that you want to match has only one underscore in it, you could use this regex:

/(#[^_]+_[^_]+)/

This matches an octothorpe (#), followed by two strings that do not contain an underscore, seperated by a single underscore.

yjerem
/(#[^_]+_[^_]+)/
joe
A: 

Something a little simpler:

(\#OnlinePopup_.*?)_

Assuming your text starts with # and ends with _

Gavin Miller