<p>What's the easiest way to do an "instring" type function with a regex? For example, how could I reject a whole string because of the presence of a single character such as <code>:</code>? For example: </p>

<ul>
<li><code>this</code> - okay</li>
<li><code>there:is</code> - not okay because of <code>:</code></li>
</ul>

<p>More practically, how can I match the following string: </p>

<pre><code>//foo/bar/baz[1]/ns:foo2/@attr/text()
</code></pre>

<p>For any node test on the xpath that doesn't include a namespace?</p>

<pre><code>(/)?(/)([^:/]+) 
</code></pre>

<p>Will match the node tests but includes the namespace prefix which makes it faulty.</p>

[{'answer_id': 10168, 'author': 'Adam Lerman', 'author_id': 673, 'author_profile': 'https://Stackoverflow.com/users/673', 'pm_score': 0, 'selected': False, 'text': '<p>I dont know regex syntax very well but could you not do:</p>\n\n<p><code>[any alpha numeric]\\*:[any alphanumeric]\\*</code></p>\n\n<p>I think something like that should work no?</p>\n'}, {'answer_id': 10186, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 1, 'selected': False, 'text': '<p>Match on :?  I think the question isn\'t clear enough, because the answer is so obvious:</p>\n\n<pre><code>if(Regex.Match(":", input)) // reject\n</code></pre>\n'}, {'answer_id': 10187, 'author': 'Mike Stone', 'author_id': 122, 'author_profile': 'https://Stackoverflow.com/users/122', 'pm_score': 0, 'selected': False, 'text': '<p>You might want \\w which is a "word" character.  From <a href="http://java.sun.com/j2se/1.5.0/docs/api/java/util/regex/Pattern.html" rel="nofollow noreferrer">javadocs</a>, it is defined as [a-zA-Z_0-9], so if you don\'t want underscores either, that may not work....</p>\n'}, {'answer_id': 10325, 'author': 't3rse', 'author_id': 64, 'author_profile': 'https://Stackoverflow.com/users/64', 'pm_score': 0, 'selected': False, 'text': '<p>Yeah, my question was not very clear. Here\'s a solution but rather than a single pass with a regex, I use a <strong>split</strong> and perform iteration. It works as well but isn\'t as elegant: </p>\n\n<pre><code>string xpath = "//foo/bar/baz[1]/ns:foo2/@attr/text()";\nstring[] nodetests = xpath.Split( new char[] { \'/\' } );\nfor (int i = 0; i &lt; nodetests.Length; i++) \n{\n    if (nodetests[i].Length &gt; 0 &amp;&amp; Regex.IsMatch( nodetests[i], @"^(\\w|\\[|\\])+$" ))\n    {\n        // does not have a ":", we can manipulate it.\n    }\n}\n\nxpath = String.Join( "/", nodetests );\n</code></pre>\n'}, {'answer_id': 10516, 'author': 'OJ.', 'author_id': 611, 'author_profile': 'https://Stackoverflow.com/users/611', 'pm_score': 2, 'selected': False, 'text': '<p>I\'m still not sure whether you just wanted to detect if the Xpath contains a namespace, or whether you want to remove the references to the namespace. So here\'s some sample code (in C#) that does both.</p>\n\n<pre><code>class Program\n{\n    static void Main(string[] args)\n    {\n        string withNamespace = @"//foo/ns2:bar/baz[1]/ns:foo2/@attr/text()";\n        string withoutNamespace = @"//foo/bar/baz[1]/foo2/@attr/text()";\n\n        ShowStuff(withNamespace);\n        ShowStuff(withoutNamespace);\n    }\n\n    static void ShowStuff(string input)\n    {\n        Console.WriteLine("\'{0}\' does {1}contain namespaces", input, ContainsNamespace(input) ? "" : "not ");\n        Console.WriteLine("\'{0}\' without namespaces is \'{1}\'", input, StripNamespaces(input));\n    }\n\n    static bool ContainsNamespace(string input)\n    {\n        // a namspace must start with a character, but can have characters and numbers\n        // from that point on.\n        return Regex.IsMatch(input, @"/?\\w[\\w\\d]+:\\w[\\w\\d]+/?");\n    }\n\n    static string StripNamespaces(string input)\n    {\n        return Regex.Replace(input, @"(/?)\\w[\\w\\d]+:(\\w[\\w\\d]+)(/?)", "$1$2$3");\n    }\n}\n</code></pre>\n\n<p>Hope that helps! Good luck.</p>\n'}]
['https://Stackoverflow.com/questions/10158', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/64/']