Regular expressions are a declarative language, mainly used for pattern matching within strings. Please include a tag specifying the programming language you are using, together with this tag.
12
votes
6answers
3k views
Use of a regex stored inside yaml file
I installed settingslogic and in the configuration file I put the regex for the email as follows
#config/settings.yml
defaults: &defaults
email_regex: ...
7
votes
5answers
298 views
Can this regex for checking strong password be improved?
^(?=^.*[a-z])(?=^.*[A-Z])(?=^.*[0-9])(\S{8,})(?<!\s)$
Is there any way to make this regex more concise? Ensures a password has lowercase, uppercase, digit and is at least 8 characters, ...
7
votes
4answers
4k views
Simplify splitting a String into alpha and numeric parts
Requirement: Parse a String into chunks of numeric characters and alpha characters. Alpha characters should be separated from the numeric, other characters should be ignored.
Example Data:
Input ...
5
votes
4answers
164 views
Best way to replace a beginning and end character in Perl using Regular Expression?
I'm wondering if there is a simplier regex I could use in my code to remove the beginning and ending char in a line. Maybe combine some regex's? In this instance, it's a comma at the beginning and ...
5
votes
1answer
245 views
Is there anyway to minimize the following code? combine regex for example?
i have built the following code using help from people at stackoverflow,
but i am pretty sure it can be minimized,but since i dont know regex very good (actually hardly), i do not know how to do it.
...
4
votes
3answers
164 views
How to determine the order of letters by regex?
I want to write a regular expression to determine whether a given string is uppercase and sorted in non-descending order.
^A*B*C*D*E*F*G*H*I*J*K*L*M*N*O*P*Q*R*S*T*U*V*Q*X*Y*Z*$
I was just wondering ...
4
votes
3answers
252 views
Checking a HTML element's class
$('pre').each(function(index) {
var c = $(this).attr("class");
if (!c) {
return true;
}
// Match only Javascript code snippets
if (!c.match(/brush: js; class-name: ...
4
votes
2answers
95 views
Refactor highlight matched word in string
I have following method which highlight matched word in text:
# Hightlight matched term
#
# Ex(for term: some):
# "<span class="bold">Some</span> CEO Event"
#
def ...
4
votes
1answer
216 views
JavaScript replace
I am not sure the square brackets are correct (although it has not yet failed some simple tests). I would also like to reduce and simplify this code to one line if practical. I think the code is self ...
4
votes
1answer
84 views
Is my PHP script/embed remover robust?
The goal of this question:
Your goal here is to find a security hole in my code which allows a user to create input that contains a script doing anything they want, without that script being stopped ...
4
votes
2answers
110 views
How to reduce this archive detection function and make it supports wildcards
The function below is called to determine if a given file is the archive of another file. I'm also looking for a way to supports wildcards. For example if the original log file is serverw3c.log and we ...
3
votes
2answers
173 views
Python function to match filenames with extension names
I have written a Python function which matches all files in the current directory with a list of extension names. It is working correctly.
import os, sys, time, re, stat
def matchextname(extnames, ...
3
votes
3answers
252 views
Pinging the user requested host - is this code insecure?
<?php
$userinput = $_GET['host'];
$e = escapeshellcmd($userinput);
$arr = (explode(".",$e));
$num = count($arr);
$times = (int)$_GET['times'];
$time = (range(1,51));
if ...
3
votes
4answers
277 views
Extending String/Regex to be simpler in c#
I'm working on some extensions to simplify the Regex object usage in C#. Regex, RegexOptions, and MatchCollection are really nasty IMO and I'm trying to extend string with some regex functionality ...
3
votes
3answers
651 views
Regular Expression replace specific characters
Does anyone know of a better / cleaner way to write the following:
GetSafeTagName(txtUserInput.text);
public static string GetSafeTagName(string tag)
{
tag = tag.ToUpper()
.Replace("'","`")
...
3
votes
1answer
80 views
Please help me review some regexes for google app engine
My app is growing from small to medium and I'm adding functions that should get reviewed.
My routing is
routes = [
(r'/', CyberFazeHandler),
(r'/vi/(eyes|mouth|nose)', ...
3
votes
1answer
153 views
Is my regex ok to parse what I want or should I change anything to make it more …
I need to classify each line as "announce, whisper or chat" once I have that sorted out I need to extract certain values to be processed.
Right now my regex is as follow:
var regex = new ...
3
votes
1answer
68 views
Count comments and lines of code in ruby
Hi I wrote a small script as an answer for a stack overflow question, that counts lines of code and comments (in C and C++ style).
f = File.open("test.txt")
loc = 0
comments = 0
while line = f.gets
...
3
votes
1answer
197 views
Unicode parsing in PHP
Firstly, apologies if this is not the correct type of question for here, I had it on the stackoverflow but it was closed with a suggestion I post here.
I’m in the process of converting from Latin 15 ...
3
votes
1answer
317 views
Simplify regular expression? (Converting Unicode fractions to TeX)
Background
I'm converting Unicode text to TeX for typesetting. In the input, I'm allowing simple fractions like ½ and ⅔ using single Unicode characters and complex fractions like ¹²³/₄₅₆ using ...
3
votes
1answer
180 views
How/where can I improve this code?
I'm quite new to Javascript, what areas of the code should I concentrate on improving, and if possible, how to improve them.
Basically, the code looks for certain emoticon characters (like :) or :|) ...
3
votes
1answer
140 views
Implementation of Python's re.split in Clojure (with capturing parentheses)
If you use capturing parenthesis in the regular expression pattern in Python's re.split() function, it will include the matching groups in the result (Python's documentation).
I need this in my ...
3
votes
1answer
217 views
I'm trying to escape some XML to be used with SOAP
I'm attempting to escape ampersands from a string before passing to PHP's SimpleXMLElement->addChild() for use with some SOAP webservices, but I don't want to double escape them. I'm getting these ...
2
votes
3answers
353 views
Tiny bash script: Is sed necessary here?
I've recently discovered the power of sed; regular expressions seem like they'll take a lifetime to master. This tiny script uses both to add zero before the single digit in file names.
Can this be ...
2
votes
2answers
35 views
Trying to improve a working regex
I am extracting data from a text file. Some of the lines from which I want to extract the data consist of a text description with single spaces, followed by a multiple-space gap preceding four fields ...
2
votes
3answers
398 views
Any suggestions on how to improve this mobile user agent check?
I've been using the following user agent Regular Expression to detect mobile devices, but I recently came across a few resources that listed a whole host of mobile user agents that I had not heard of ...
2
votes
2answers
205 views
Brace pairing ({}[]()<>) cleanup/speedup
This is working as expected, except in the speed area + I need to make it more readable and shorter if possible, I probably have lot's of things I don't need :).
Edit it is working now
charset = ...
2
votes
3answers
1k views
Ruby code style / Regular Expression to remove blank lines / test case
Forgive me to put 3 seemingly irrelevant words in the subject line. But
they are the purposes of this post.
This is basically my first Ruby class, which is to remove all empty
lines and blank times ...
2
votes
1answer
148 views
More concise way to write this clojure code adapted from map-invert?
I wanted to do the following:
Count the frequencies of words in a text (over 5 letters)
Invert the map of words to frequencies, but group together words that have the same frequency in the ...
2
votes
1answer
492 views
Evaluate/Refactor my ASP.NET XSS Security Helper Class
I am maintaining a site with significant security concerns and I wrote a helper class for validating potential XSS attacks. The ValidateRequest method is meant to evaluate the HttpRequest for any ...
2
votes
2answers
696 views
String pattern matching - method needs improving
I'm trying to build simple pattern matching in a string.
ie., Given a template of: "Hello ${1}" and an input of "Hello world", I'd like to be able to return a map of 1='world'.
This test ...
2
votes
2answers
231 views
Code Improvement: Copy Regex named capture groups to strongly typed object
This code works to copy named capture groups of a Regex to the same named properties of a strongly typed object. However, it's not pretty. Someone mentioned I should use polymorphism to at least ...
2
votes
1answer
31 views
Encapsulate results in div tags
I would like to turn the following:
<div>this is a $test</div>
into
<div>this is a <div>$test</div></div>
currently I have
var regexp = new ...
2
votes
2answers
100 views
Want to sharpen my Python / Regex skills
I have a relatively simple project to parse some http server logs using Python and SQLite. I wrote up the code but I'm always looking for tips on being a better Python scripter. Though this is a ...
2
votes
1answer
70 views
Can I make a regex array to iterate through in C++?
I have to check a string to various regular expressions in C++. Up to now, I've done this using something similar to this:
regex regex_a (".."); string rewrite_a = "($1/$2)";
regex regex_b (".."); ...
2
votes
1answer
107 views
filter out password in xml string
We're storing the xml communications with an external service in a text in the db and in a before_create I've got the following:
# filter opera password
def remove_password!
...
2
votes
1answer
905 views
Converting JSONP to JSON: Is this Regex correct?
I'm fetching a string that is JSONP, and looking to convert it to JSON.
I'm using the following regular expression for matching (for removal) the padding on the JSON.
([a-zA-Z_0-9\.]*\()|(\);?$)
...
2
votes
1answer
78 views
Capturing optional regex segment with PHP
I need to check the end of a URL for the possible existence of /news_archive or /news_archive/5 in PHP. The below snippet does exactly what I want, but I know that I could achieve this with one ...
2
votes
2answers
144 views
Add getters and setters to a set of variables
Any improvements are welcome.
a.bat
sed -r "s/private (.*) (.*);(.*)/&\npublic \1 get\u\2(){return \2;}\npublic void set\u\2(\u\2){this.\2=\u\2;}\n/" "%~1">"%~2"
in:
private String ...
2
votes
2answers
130 views
Bulleted list from dashes
Let's keep this simple.
Input:
- abc
- def
- ghi
Desired output (whitespace optional):
<ul>
<li>abc</li>
<li>def</li>
<li>ghi</li>
...
2
votes
2answers
197 views
Replace match inside tags
The problem I want to solve is to replace a given string inside tags.
For example, if I'm given:
Some text abc [tag]some text abc, more text abc[/tag] still some more text
I want to replace abc ...
2
votes
1answer
53 views
RegEx Code Organization
I've written an algebraic notation to use for moves in a game and I'm currently writing code to parse the information from the notation. I'm doing this using regExes. This is coded in HaXe.
For ...
2
votes
1answer
75 views
Regular expression for application version
Only these formats are accepted.
1.1.1
1.1.1-r
1.1.1-b
1.1.1-r1
1.1.1-b1
I wrote this code. What don't I like in it? I used parentheses and now I have two groups. In fact, I don't need to do ...
2
votes
3answers
879 views
Better implementation of a simplified regular expression engine?
Task:
On an alphabet set [a-z], a simplified regular expression is much simpler than the normal regular expression.
It has only two meta characters: '.' and '*'.
'.' -- exact one arbitrary ...
2
votes
0answers
238 views
Processing XML configuration which stores regular expressions and format strings for a documentation tool
I'm investigating some feature for the ScalaDoc tool, which would allow library writers to link to documentation created by third party tools like JavaDoc.
My idea is to have some (XML) configuration ...
1
vote
1answer
154 views
Faster JavaScript fuzzy string matching function?
I'm using the following function to fuzzy match strings:
function fuzzy_match(str,pattern){
pattern = pattern.split("").reduce(function(a,b){ return a+".*"+b; });
return (new ...
1
vote
3answers
216 views
Regex to get all image links - is this efficient?
I have some pretty basic Regex that scans the output of a HTML file (the whole document source) and attempts to extract all of the absolute links that look like images. Whether they are actually ...
1
vote
3answers
100 views
Best Regular Expression for a one or two values optionally divided by a certain character
I'm looking for the most concise regex that matches one or two 4-digit years in any the following setups:
year
year-
-year
year-year
I can't think of anything slicker than this:
...
1
vote
2answers
69 views
php most efficient way to check if a variable contains only certain chars
I have a small function which I regularly use to check if a variable contains only [a-z][0-9] and the special chars '-' and '_'. Currently I'm using the following:
function is_clean($string){
...
1
vote
2answers
39 views
How to make this Regex more flexible?
I am creating a simple template engine that uses Regex to find special expressions, which are parsed and processed. They are enclosed in Ruby-style opening tags and have the format:
<% label ...
