Swift regex for characters and empty spaces - regex

I'm trying a regex expression to only allow characters and spaces for a full name field i.e. Mr Bob Smith
What I've currently tried:
let textRegex = "[A-Za-z+\\s]"
let textRegex = "[A-Za-z ]"
let textRegex = "[A-Za-z+ ]"
let textRegex = "([A-Za-z ])"
It doesn't appear to be working.
Thanks

Your regular expression isn't working because you misplaced the + symbol.
This one will work:
([A-Za-z ]+)
I don't know how Swift handles regex however so keep in mind if you strictly want whitespaces only, it is better to just add " " character instead of the \s which can sometimes be extended to other spaces.

Related

Replacing STX and ETB characters, in VB6, using Regex

I'm reading COM port results using a vb6 application, and I need to replace some characters, using regex expressions.
The issue is primarily this: I'm getting a lot of unnecessary characters between the "R" and "|" characters, which I'd like to remove. For this, I'm using the replace function and regex expressions, but it's not working.
This is the code I've written in vb6:
objReg.Pattern = "R.*\|"
objReg.Global = True
x$ = objReg.Replace(Text1.Text, "R|")
Input Stream:
RDA
3|4|
which is ("R" + ETB + "DA" + STX + "3|4|")
Expected Result:
R|4|
Any help in this regard would be much appreciated, thanks!
You may use
objReg.Pattern = "R[^|]+\|"
x$ = objReg.Replace(Text1.Text, "R|")
See the regex demo
The regex will match R, then one or more chars other than | (with the [^|]+ pattern) and then a literal | char. The whole match will be replaced with R|.
You may also use capturing groups with backreferences here if you need to make any more additions to the pattern:
objReg.Pattern = "(R)[^|]+(\|)"
x$ = objReg.Replace(Text1.Text, "$1$2")
The (R) group will correspond to the $1 backreference and (\|) will correspond to $2.
See another regex demo.

Replace pattern with pattern in vb.net string

I want to replace "0A ","0B ",...,"1A ","1B ",... patterns with "0A|","0B|",...,"1A|","1B|",... from string vb.net
I can write individual replace lines like
string = string.Replace("0A ", "0A|")
string = string.Replace("0B ", "0B|")
.
.
.
string = string.Replace("0Z ", "0Z|")
But, I would have to write too many lines(26*10*2- Two because such scenario occurs twice) and it just doesn't seem to be a good solution. Can someone give me a good regex solution for this?
Use Regex.Replace:
result = Regex.Replace(string, "(\d+[A-Z]+) ", "$1|")
I used the pattern \d+[A-Z]+ to represent the text under the assumption that your series of data might see more than one digit/letter. This seems to be working in the demo below.
Demo
Regex: \s Substitution: |
Details:
\s Matches any whitespace character
Regex demo
VB.NET code:
Regex.Replace("0A ", "\s", "|") Output: 0A|

What is a Regex equivalent of the 'Trim' function used in languages such as VB and Java..?

I'm using Regex in a Microsoft Access 2007 database with a VBA project reference to Microsoft VBScript Regular Expressions 5.5.
All is well...mostly. I would like to know a Regular Expression that will act like the 'Trim' function..? (remove leading and trailing spaces)
I have this: ((?:.?)*) which is to "capture everything after the last match". But it always matches extra spaces which I would like to remove.
Below is the relevant code, followed by a screenshot of the debugger. Item 4 in the submatches has " CAN". How do I remove the space with Regex, so I don't have to use the Trim function..?
pattern = "^(\d{1,2})(?:\/)(\d{1,2}(?:\.\d{1,3})?)(OZ)((?:.?)*)"
regex.pattern = pattern
Set matchCollection = regex.Execute(workstring)
If matchCollection.Count > 0 Then
matchSection = "LOOSE CASES"
itemtype = "CASE"
percase = matchCollection(0).SubMatches(0)
perpack = 1
unitsize = matchCollection(0).SubMatches(1)
uom = matchCollection(0).SubMatches(2)
other = VBA.Trim(matchCollection(0).SubMatches(3))
End If
...
Ok, I finally figured it out. To reiterate (and clarify): my original regex ((?:.?)*) is meant to "capture anything left after the last match". But it also captured leading & trailing spaces.
Removing the leading spaces was fairly easy, but every attempt to remove the trailing spaces was foiled by the * in the group. Then I read about \b and dropped one in and now it works.
This is what I have now: (?: ?)((?:.?)*)\b(?: *) which is "match anything left after the last match, except leading or trailing spaces".
And in context, this is the whole of it...
(\d{1,2})/(\d{1,2})PK-(\d{1,2}(?:.\d{1,3})?)(OZ|ML)(?: ?)((?:.?)*)\b(?: *)
Which is meant to match on a string such as this...
2/12PK-11.125OZ CAN RET
...which describes cases of beer in our warehouse. =-)

How to better this regex?

I have a list of strings like this:
/soccer/poland/ekstraklasa-2008-2009/results/
/soccer/poland/orange-ekstraklasa-2007-2008/results/
/soccer/poland/orange-ekstraklasa-youth-2010-2011/results/
From each string I want to take a middle part resulting in respectively:
ekstraklasa
orange ekstraklasa
orange ekstraklasa youth
My code here does the job but it feels like it can be done in fewer steps and probably with regex alone.
name = re.search('/([-a-z\d]+)/results/', string).group(1) # take the middle part
name = re.search('[-a-z]+', name).group() # trim numbers
if name.endswith('-'):
name = name[:-1] # trim tailing `-` if needed
name = name.replace('-', ' ')
Can anyone see how make it better?
This regex should do the work:
/(?:\/\w+){2}\/([\w\-]+)(?:-\d+){2}/
Explanation:
(?:\/\w+){2} - eat the first two words delimited by /
\/ - eat the next /
([\w\-]+)- match the word characters of hyphens (this is what we're looking for)
(?:-\d+){2} - eat the hyphens and the numbers after the part we're looking for
The result is in the first match group
I cant test it because i am not using python, but i would use an Expression like
^(/soccer/poland/)([a-z\-]*)(.*)$
or
^(/[a-z]*/[a-z]*/)([a-z\-]*)(.*)$
This Expressen works like "/soccer/poland/" at the beginning, than "everything with a to z (small) or -" and the rest of the string.
And than taking 2nd Group!
The Groups should hold this Strings:
/soccer/poland/
orange-ekstraklasa-youth-
2010-2011/results/
And then simply replacing "-" with " " and after that TRIM Spaces.
PS: If ur Using regex101.com e.g., u need to escape / AND just use one Row of String!
Expression
^(\/soccer\/poland\/)([a-z\-]*)(.*)$
And one Row of ur String.
/soccer/poland/orange-ekstraklasa-youth-2010-2011/results/
If u prefere to use the Expression not just for soccer and poland, use
^(\/[a-z]*\/[a-z]*\/)([a-z\-]*)(.*)$

Regular expression extract filename from line content

I'm very new to regular expression. I want to extract the following string
"109_Admin_RegistrationResponse_20130103.txt"
from this file content, the contents is selected per line:
01-10-13 10:44AM 47 107_Admin_RegistrationDetail_20130111.txt
01-10-13 10:40AM 11 107_Admin_RegistrationResponse_20130111.txt
The regular expression should not pick the second line, only the first line should return a true.
Your Regex has a lot of different mistakes...
Your line does not start with your required filename but you put an ^ there
missing + in your character group [a-zA-Z], hence only able to match a single character
does not include _ in your character group, hence it won't match Admin_RegistrationResponse
missing \ and d{2} would match dd only.
As per M42's answer (which I left out), you also need to escape your dot . too, or it would match 123_abc_12345678atxt too (notice the a before txt)
Your regex should be
\d+_[a-zA-Z_]+_\d{4}\d{2}\d{2}\.txt$
which can be simplified as
\d+_[a-zA-Z_]+_\d{8}\.txt$
as \d{2}\d{2} really look redundant -- unless you want to do with capturing groups, then you would do:
\d+_[a-zA-Z_]+_(\d{4})(\d{2})(\d{2})\.txt$
Remove the anchors and escape the dot:
\d+[a-zA-Z_]+\d{8}\.txt
I'm a newbie in php but i think you can use explode() function in php or any equivalent in your language.
$string = "01-09-13 10:17AM 11 109_Admin_RegistrationResponse_20130103.txt";
$pieces = explode("_", $string);
$stringout = "";
foreach($i = 0;$i<count($pieces);i++){
$stringout = $stringout.$pieces[$i];
}