Here is the string I want to match 76c24efd-ec42-492a-92df-c62cfd4540a3. The following regex will match a 36 char length string with alphanumeric characters and '-'.
[a-zA-Z0-9\-]{36}
I am trying to add to this regex, so it matches only when <8 chars> - <4 chars> - <4 chars> - <4 chars> - <12 chars>
The following will match a group of 8 characters followed by a dash, then a group of 4 characters followed by a dash 3 times, and then a group of 12 characters.
^[a-z0-9]{8}-(?:[a-z0-9]{4}-){3}[a-z0-9]{12}$/
Here's a short JavaScript test that shows the results.
$('.test').each(function(row, item) {
var val = $($(item).children()[0]).html()
var result = /^[a-z0-9]{8}-(?:[a-z0-9]{4}-){3}[a-z0-9]{12}$/gi.test(val);
$($(item).children()[1]).html(result.toString())
});
th {
text-align:left;
padding-right:10px;
}
td {
border: 1px solid #ccc;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table style="border: 1px solid black">
<tr><th>Test</th><th>Result</th><th>Desired Result</th></tr>
<tr class="test"><td>76c24efd-ec42-492a-92df-c62cfd4540a3</td><td></td><td>Good</td></tr>
<tr class="test"><td>76c24efd-ecz42-492a-92df-c62cfd4540a3</td><td></td><td>Bad Length - 2nd segment too long</td></tr>
<tr class="test"><td>76c24efd-ec2-492a-92df-c62cfd4540a31</td><td></td><td>Bad Segment Lengths</td></tr>
<tr class="test"><td>76$24efd-ec42-492a-92df-c62cfd4540a3</td><td></td><td>Bad Char ($)</td></tr>
</table>
Related
I fetched data from a web sites body.Then I write a regular expression and applied on DART but it didnt work.What is the Problem?
Here is the Regex code:
</td><td align="left">(.*?)</td><td class="dataGridActive
Here is my part of the content:
</tr><tr onmouseover="mover(this);" onmouseout="mout(this);" style="background-color:White;">
<td align="left">233</td><td align="left">ÖMER EFE CIKIT</td><td class="dataGridActive" align="center">
And the dart code:
void CheckRE(String text) {
final RegExp pattern = RegExp(
r'</td><td align="left">(.*?)</td><td class="dataGridActive"',
multiLine: true,
caseSensitive: true,
); // 800 is the size of each chun
pattern
.allMatches(text)
.forEach((RegExpMatch match) => print(match.group(1)));
}
I think what you want is the following.
I have changed your output so it prints the content of capture group 1 instead of capture group 0. Capture group 0 contains the whole string which matches while 1 and up contains the content of each defined capture group in your regular expression.
const input = '''
</tr><tr onmouseover="mover(this);" onmouseout="mout(this);" style="background-color:White;">
<td align="left">233</td><td align="left">ÖMER EFE CIKIT</td><td class="dataGridActive" align="center">
''';
void main() => checkRE(input); // ÖMER EFE CIKIT
void checkRE(String text) {
final RegExp pattern = RegExp(
r'</td><td align="left">(.*?)</td><td class="dataGridActive"',
multiLine: true,
caseSensitive: true,
); // 800 is the size of each chun
pattern.allMatches(text).forEach((RegExpMatch match) => print(match[1]));
}
Also changed (.*) to (.*?) based on advice from #MikeM.
I made a ans ASP on wich a user can enter a number, one or more characters, and a word. Then he can press the button and it replaces in the given characters in the word with the given numbers.
My question is how can I let the script replace single characters when the user entered more?
i.e. Entry is "ab" the word is "abby", and the number is "1", my current program makes "1by", but I want to make it "111y", how do I realize that?
<html>
<head>
<meta charset = "utf-8">
<title>Replace
</title>
</head>
<body>
<%
response.flush
l_zahl = request.querystring("f_zahl")
l_wort = request.querystring("f_wort")
Dim letterarray, l_letter
l_letter = request.querystring("f_letter")
letterarry = Split("l_letter")
If IsNumeric(request.querystring("f_zahl")) And Not IsNumeric(request.querystring("f_letter")) And Not IsNumeric(request.querystring("f_wort")) Then
Dim zahlarray, l_zahl
l_zahl = request.querystring("f_zahl")
zahlarry = Split("l_zahl")
Dim wortarray, l_wort
l_wort = request.querystring("f_wort")
wortarry = Split("l_wort")
l_replace = (Replace(l_wort, l_letter, l_zahl, 1, -1, 1))
ElseIf Not IsNumeric(request.querystring("f_zahl")) Then
l_replace = "Keine Zahl"
ElseIf IsNumeric(request.querystring("f_letter")) Then
l_replace = "Kein Buchstabe"
ElseIf IsNumeric(request.querystring("f_wort")) Then
l_replace = "Kein leetspeak"
End If
%>
<form action = "Replacer.asp" method = "get">
<table width = "800" heigth = "400" border="1" cellspacing="0" cellpadding="1" align = "center" font face="tahoma, arial, helvetica, sans-serif" >
<tr>
<td align = "left" width = "100">
Bitte Zahl eingeben
</td>
<td align = "left" width = "100">
<input type = "text" name = "f_zahl" value = "<%=l_zahl%>">
</td>
<td align = "left" width = "100">
Bitte Buchstabe eingeben
</td>
<td>
<input type = "text" name = "f_letter" value = "<%=l_letter%>">
</td>
<td>
Bitte ein Wort eingeben
</td>
<td align = "left" width = "100">
<input type = "text" name = "f_wort" value = "<%=l_wort%>">
</td>
<td width = "*">
<input type = "submit" value = "Ersetzen" \>
</td>
</tr>
<tr>
<td colspan = "2">
</td>
<td >
Verändertes Wort
</td>
<td colspan = "4">
<%=l_replace%>
</td>
</tr>
</table>
</body>
</html>
The Replace function replaces the search string (l_letter) in the given expression (here l_wort) with the replacement string (l_zahl). To replace all characters in l_letter with l_zahl you need to do the replacement in a loop for each character in l_letter. However, the Split function doesn't allow you to split a string into an array of its characters. It splits a string at a given delimiter character (space by default). Calling Split on a variable without a space will give you an array with just a single field containing the original string. Also, VBScript doesn't expand variables in strings, so if you put variable in double quotes you'll get the literal string "variable", not a string with the value of the variable.
var = "ab" : Split("var") ⇒ [ "var" ]
var = "ab" : Split(var) ⇒ [ "ab" ]
var = "a b" : Split(var) ⇒ [ "a", "b" ]
For extracting individual characters from a string use the Mid function:
l_replace = l_wort
For i=1 To Len(l_letter)
l_replace = Replace(l_replace, Mid(l_letter, i, 1), l_zahl)
Next
A better approach than doing multiple replacements in a loop would be using a regular expression replacement:
Set re = New RegExp
re.Pattern = "[" & l_letter & "]"
l_replace = re.Replace(l_wort, l_zahl)
Treat the character input as a list/collection of letters to replace:
Option Explicit
Dim f : f = "ab"
Dim t : t = "1"
Dim w : w = "abby"
WScript.Echo f, t, w
Dim i
For i = 1 To Len(f)
w = Replace(w, Mid(f, i, 1), t)
Next
WScript.Echo f, t, w
output:
cscript 47469843.vbs
ab 1 abby
ab 1 111y
I'm using this Regex:
padding:(\s*(\d+px\s*|0\s*)){4}
to find padding style's with four values in Visual Studio "Find And Replace".
Now I want to replace the second value with fourth like this:
padding: 1px 0 3px 4px;
padding: 1px 2px 0 4px;
padding: 1px 2px 3px 0;
change this values to:
padding: 1px 4px 3px 0;
padding: 1px 4px 0 2px;
padding: 1px 0 3px 2px;
is there a way I can replace them with the "Find And Replace" dialog?
Note that (\s*(\d+px\s*|0\s*)){4} captures the (\s*(\d+px\s*|0\s*)) subpattern 4 times, and the captures are stored in the CaptureCollection that can only be retrieved with .NET code. In a search and replace feature, you can only access groups with backreferences.
You can use:
Find: (padding:\s*)(\d+(?:px)?)\s*(\d+(?:px)?)\s*(\d+(?:px)?)\s*(\d+(?:px)?);
Replace: $1$2 $5 $4 $3
See demo
Results:
padding: 1px 4px 3px 0
padding: 1px 4px 0 2px
padding: 1px 0 3px 2px
Note I decided to hard-code the whitespaces in the replacement, but you can also set capturing groups round \s* to replicate them in the resulting string.
Regex breakdown:
(padding:\s*) - Group 1 matching padding: plus 0 or more whitespace
(\d+(?:px)?) - Group 2 matching 1 or more digits (\d+) and optionally px
\s* - 0 or more whitespaces
(\d+(?:px)?)\s*(\d+(?:px)?)\s*(\d+(?:px)?) - 3 more capturing groups with indices 3, 4, 5
; - a literal semi-colon.
I'm trying to write a regex that matches any semicolon symbol ; that doesn't start with !important or !important
For instance, if I have the following string:
.myclass {
width: 400px;
color: #333;
margin: 20px !important;
padding: 20px !important ;
top: 1px;
}
I want to match those lines:
width: 400px;
color: #333;
top: 1px;
So I can then run replace on them and add the !important attribute to them.
How should I write the regex that would match this?
Try using this one: (?!.*!important).*;.
Breaking it down into smaller pieces we are using a negative lookahead (?!<pattern>) to say we want to match where there is NOT the match later in the string. After that, we just need to look for any chars up until we see a closing ;. The way the negative lookahead is setup, if the line is ending in the ; and there is a match to !important at all it will fail, no matter how many spaces are inbetween. Since CSS can have spaces, this handles a lot more cases you could see other then 0 or 1 spaces.
If you wanted it to be EXACTLY like the original post where you are checking for zero or one space after !important but before ;, you can change the lookahead to include \s?;, after !important of course. This is checking for ANY whitespace, zero or one of, followed directly by the ;.
This one worked for me at a Regex tester:
.+[^((?!important)\s];
The regex matches any number of characters (.+) except the ones with !important in it ([^!important]).
If this is entirely in one string variable
.myclass {
width: 400px;
color: #333;
margin: 20px !important;
padding: 20px !important ;
top: 1px;
}
then you can split it on the new-line:
String[] lines = input.split(System.getProperty("line.separator", "\r\n"));
Then, skipping the first and last elements (which contain the curly-braces), only get lines that do not match "!important ?;"
Matcher importantMtchr = Pattern.compile("!important ?;").matcher("");
for(int i = 1; i < lines.length - 1; i++) {
String line = lines[i];
if(!importantMtchr.reset(line).find()) {
System.out.println(line);
}
}
Full code:
import java.util.regex.Pattern;
import java.util.regex.Matcher;
public class NotExclamationImportantLines {
public static final String LINE_SEP = System.getProperty("line.separator", "\r\n");
public static final void main(String[] ignored) {
String input = new StringBuilder().
append("myclass {" ).append(LINE_SEP).
append("width: 400px;" ).append(LINE_SEP).
append("color: #333;" ).append(LINE_SEP).
append("margin: 20px !important;" ).append(LINE_SEP).
append("padding: 20px !important ;").append(LINE_SEP).
append("top: 1px;" ).append(LINE_SEP).
append("}" ).toString();
//Split on the newline
String[] lines = input.split(LINE_SEP);
//Skip over the first and last elements, and only
//print out those that don't contain the regular
//expression `"important! ?"
Matcher importantMtchr = Pattern.compile("!important ?;").matcher("");
for(int i = 1; i < lines.length - 1; i++) {
String line = lines[i];
if(!importantMtchr.reset(line).find()) {
System.out.println(line);
}
}
}
}
Output:
width: 400px;
color: #333;
top: 1px;
^(?!.*!important\s*;).*?(;)\s*$
^^^^^^^^^^^^^^^^ ^
(1) (2)
Negative Lookahead assertion: Find lines that do not contain !important followed by zero or more whitespace characters followed by a semicolon.
Capture Group: In the lines that pass the negative-lookahead test, capture only a semicolon at the end that is followed by zero or more whitespace characters.
See the live demo.
I have some HTML source that i get from a website for option quotes. (please see below)
What is the best way to extract the various text values in tr and store in a collection based on the strike price (4700 in this case available in the mid td 4700.00)
Some people recommend regex while other suggest to use a html parser. I'm doing this in VBA so whats the best way?
<!--<td>Quote</td>
<td><img src="/images/print3.gif">
</td>-->
<td><img src="/live_market/resources/images/grficon.gif" /></td>
<td class="ylwbg"> 2,935,500</td>
<td class="ylwbg"> 27,550</td>
<td class="ylwbg"> 12,458</td>
<td class="ylwbg"> 23.79</td>
<!-- End-->
<td class="ylwbg">
139.25
</td>
<!--*Net Change*-->
<td class="ylwbg" Style="color:Red;"> -7.35</td>
<td class="ylwbg"> 200</td>
<td class="ylwbg"> 139.15</td>
<td class="ylwbg"> 142.45</td>
<td class="ylwbg"> 200</td>
<td class="grybg"><b>4700.00</b></td>
<td class="nobg"> 1,300</td>
<td class="nobg"> 76.00</td>
<td class="nobg"> 79.00</td>
<td class="nobg"> 1,350</td>
<!--*Net Change*-->
<td class="nobg" Style="color:Red;"> -1.55</td>
<td class="nobg">
<!-- 76.00 -->
76.00
</td>
<td class="nobg"> 26.33</td>
<td class="nobg"> 32,772</td>
<td class="nobg"> 103,700</td>
<td class="nobg"> 5,123,300</td>
<td><img src="/live_market/resources/images/grficon.gif" /></td>
<!--<td>Quote</td>
<td><img src="/images/print3.gif"></td>-->
</tr>
After some fiddling I have derived a regex/VBA solution using
XMLHTTP to access the site (change strSite to suit)
a Regexp to get the required numbers
a variant array with 20 records to hold, then dump the numbers to the active sheet
Looking at the source HTML to find Regex patterns
The Call options have a common starting and finishing string that delimit the 10 values, but there are three different strings
Strings 1-4,7-10 for each record match <td class="ylwbg">X</td>
String 6 has a Style (and other text) preceding the > before the X
String 5 contains a much longer <a href textX</a>
A regex of
.Pattern = "(<tdclass=""ylwbg"")(Style.+?){0,1}>(.+?)(<\/td>)"
extracts all the needed strings, but further work is needed later on string 5
The Put options start with <td class="nobg" so these are happily not extracted by a regex that gets points 1-3
Actual Code
Sub GetTxt()
Dim objXmlHTTP As Object
Dim objRegex As Object
Dim objRegMC As Object
Dim objRegM As Object
Dim strResponse As String
Dim strSite As String
Dim lngCnt As Long
Dim strTemp As String
Dim X(1 To 20, 1 To 10)
X(1, 1) = "OI"
X(1, 2) = "Chng in vol"
X(1, 3) = "Volume"
X(1, 4) = "IV"
X(1, 5) = "LTP"
X(1, 6) = "Net Chg"
X(1, 7) = "Bid Qty"
X(1, 8) = "Bid Price"
X(1, 9) = "Ask Price"
X(1, 10) = "Ask Qnty"
Set objXmlHTTP = CreateObject("MSXML2.XMLHTTP")
strSite = "http://nseindia.com/live_market/dynaContent/live_watch/option_chain/optionDates.jsp?symbol=NIFTY&instrument=OPTIDX&strike=4700.00"
On Error GoTo ErrHandler
With objXmlHTTP
.Open "GET", strSite, False
.Send
If .Status = 200 Then strResponse = .ResponseText
End With
On Error GoTo 0
Set objRegex = CreateObject("vbscript.regexp")
With objRegex
'*cleaning regex* to remove all spaces
.Pattern = "[\xA0\s]+"
.Global = True
strResponse = .Replace(strResponse, vbNullString)
.Pattern = "(<tdclass=""ylwbg"")(Style.+?){0,1}>(.+?)(<\/td>)"
If .Test(strResponse) Then
lngCnt = 20
Set objRegMC = .Execute(strResponse)
For Each objRegM In objRegMC
lngCnt = lngCnt + 1
If Right$(objRegM.submatches(2), 2) <> "a>" Then
X(Int((lngCnt - 1) / 10), IIf(lngCnt Mod 10 > 0, lngCnt Mod 10, 10)) = objRegM.submatches(2)
Else
'Get submatches of the form 206.40
strTemp = Val(Right(objRegM.submatches(2), Len(objRegM.submatches(2)) - InStrRev(objRegM.submatches(2), """") - 1))
X(Int((lngCnt - 1) / 10), IIf(lngCnt Mod 10 > 0, lngCnt Mod 10, 10)) = strTemp
End If
Next
Else
MsgBox "Parsing unsuccessful", vbCritical
End If
End With
Set objRegex = Nothing
Set objXmlHTTP = Nothing
[a1].Resize(UBound(X, 1), UBound(X, 2)) = X
Exit Sub
ErrHandler:
MsgBox "Site not accessible"
If Not objXmlHTTP Is Nothing Then Set objXmlHTTP = Nothing
End Sub