RegExp Remove content outside of commas [closed] - regex

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 8 years ago.
Improve this question
Alright, so I have a database where you can get information from that'll show off in this kind of way:
ID, Display name, Likes Cake, Likes Coffee, Likes Dogs
So if you get the information, it would show something a little like to this:
1,anonymous,1,0,1
Now it's not very popular so I would like to show the people who has answered this so I would like the "1,!anonymous!,1,0,1" (anything outside the !'s) gone. I looked around and found a RegExp code that would remove stuff outside quotes, but it's rather hard and I'm rather impatient to put all the display names in quotes.
So if there was a RegExp that would erase the numbers so I could put the usernames up, would be delicious.

Well, you could do something like this:
Replae '^[^,]+([^,]+).*' With '$1'
How it looks exactly in your language may vary, of course.
But in your case this looks like CSV, so isn't parsing the CSV file easier in that case? E.g. in PowerShell you could do
Import-Csv foo.csv | select 'Display name'
and likewise for other languages that have such parsing built-in somewhere. Besides, most other options may break depending on the input because fields in CSV may contain commas too which breaks both above regex and a naïve splitting method.

You can split the database result string and then get the relevant array index.
string dbString = "1,anonymous,1,0,1";
string username = dbString.Split(',')[1];
//value of username will be "anonymous"

Related

Questions about using regular expressions [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I'm trying to learn more about regex and I'm running into a block
my current query:
function telephoneCheck(str) {
return str.match(/[0-9]{3}[-][0-9]{3}[-][0-9]{4}/g)? true : false
}
This will only work for a specific inputs such as "555-555-5555", but for other inputs such as "1 (555) 555-5555" it will not. I'm at a loss on how to query for optional characters and whitespace. Moreover bracket handling is odd and I've found some crazy queries such as /(\d+-)\1\d{4}/g but I have no idea what its doing and I don't want to use code I don't understand.
Can someone show me a query that solves for "1 (555) 555-5555" where the first two characters (the one and space) are optional inputs?
These are inputs that the regex should be able to handle:
"1 (555) 555-5555"
"1(555)555-5555"
"1 555-555-5555"
"555-555-5555"
"(555)555-5555"
"5555555555"
I found a solution
regex: function telephoneCheck(str) {
var regex = /^(1\s?)?(\(\d{3}\)|\d{3})[\s\-]?\d{3}[\s\-]?\d{4}$/;
return regex.test(str);
}
telephoneCheck("555-555-5555");
But I have no idea whats going on in here. If someone could explain whats happening I'd be happy to give you the answer for this posted question :)
You have be wary of trying to be all things within regex and question why the data is so varied in the first place.
If you are just parsing a bunch of what you are thinking should be phone numbers for example and notice a lot of different formats it might actually be more readable to use logic.
There is probably a really clever way of doing the above but I tend to be a bit more brute force with regex until I need more.
The below combines both patterns in to one regex expression. You use the | separator to say or. Also if your strings are exactly as you say, you should to use the ^ (starts with) and $ ends with to ensure you don't get false positives.
var pattern = /^[0-9] \([0-9]{3}\) [0-9]{3}-[0-9]{4}$|^[0-9]{3}[-][0-9]{3}[-][0-9]{4}$/
pattern.test('555-555-5555') //true
pattern.test('1 (555) 555-5555') // true
pattern.test('(555) 555-5555') // false
And as I say if you have lots of different formats in one. Question why, is there a way to clean things up first. Then perhaps use logic and separate statements.
var parensPattern = /^[0-9] \([0-9]{3}\) [0-9]{3}-[0-9]{4}$/
var noParensPattern = /^[0-9]{3}[-][0-9]{3}[-][0-9]{4}$/
if(parensPattern.test('1 (555) 555-5555')) {
// do something
} else if (noParensPattern.test('555-555-5555)) {
// do something
}
Check out http://regex101.com, it is a great resource.

How to replace in a particular region in many files in Perl? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I am working in many xml files. And I want to replace some particular content only in a specific region of all files. For example:
the files may have many of the following contents:
<h2>Content comes here</h2>
Now I want to replace a word only in the above <h2>...</h2> region in all files.
Please advice. Thanks in advance.
General text replacement in Perl is usually done using regexes and the s/// operator. However it is considered very unadvisable to try to interpret the structure of an XML file using only regexes.
You should use a module which parses XML. XML::Simple will allow you to load the whole document as a Perl object (using hashrefs for attributes and subtags, etc.) and you can then traverse it and do the replacement you want to. However you then have to write that structure back as you choose.
XML::Parser is a good bet in my opinion. It is conceptually a bit more tricky, but is designed to do exactly the sort of thing you want. You set up handler functions which get called every time the parser finds the start or end of a tag. In your case all these have to do is output the tag and its contents, except when it's a h2 tag, in which case you do some extra processing.
There are also some DOM-oriented parsers which you might want to use if you are used to doing stuff like this in JavaScript or some other DOM-based XML library.
Last and for the sake of completeness, you can probably write a (very short) XSLT file which will do this transformation (not an expert, so not sure exactly how) and apply it using XML::XSLT, basically in one line.

Regex: finding a number between a range with decimals [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 8 years ago.
Improve this question
I can not for the life of me get my head around this regex stuff after a few days of fiddling around I find myself seeking help from those wiser than I. Could any of you kind souls write me a line(s) that will find and match a number between 0.00 and x.xx? I do need the decimals however so hopefully this can be done.
I actually tried using
(\b|^)(0.00|0.01|0.02)(\b|$)
until x.xx and so forth but I couldn't fit the rest of it in because I need it to go into the 100.00+. Would anyone mind whipping something up real quick for me? : ) I would appreciate it more than you can imagine! Thanks very much for your time.
Ray.
Edit:
So i forgot to explain what I'm trying to achieve here, I'm using it in conjunction with a Chrome addon called Page Monitor (life saver folks try it out when you have time to kill!) which pings every time an a website updates, this also works for shares but I'm trying to make it only alert me when the price drops below a certain point eg $4.99 per share, will (\b|^)([0-9]+\.[0-9]{2})(\b|$) and ([0-9]+.[0-9]+) suffice?
Why isn't this good enough: ([0-9]+\.[0-9]+) ?
If you can give an example of input and what is the output you expect, it would be easier to write a regex.
Updated: $ sign is a reserved character in RegEx, it means end-of-line, so you need to use \$, if you plan on using it.
So your regex would be \$([0-9]+\.[0-9]+), this would capture your $4.99 and $5.10, etc, not just $4.99
Regexs in general are good at capturing data, less at analyzing it, but if you must, you can do this to determine when the price goes below $4.99 =>
\$(([0-3]\.[0-9]+)|(4\.[0-8][0-9])|(4\.9[0-8]))
It should be obvious that its a waste of resource :)
Didn't provide enough info but this will match if the number is the entire value or if it is within a larger string and the number is not withing something else like "foo8.9bar". This will match any 1 or more digit number on the left side of the decimal and exactly 2 numbers on the right side
(\b|^)([0-9]+\.[0-9]{2})(\b|$)
(\b|^) and (\b|$) are redundant because \b implies ^ and $.
this regex: (\d+\.\d{2}) should do it.

Replace Specific Instance [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
I'm completely new to RegEx and could really use some help with my delimma. I have a large text file of IP addresses and corresponding Hosts.
eg.
157.55.33.47 msnbot-157-55-33-47.search.msn.com
157.56.93.62 msnbot-157-56-93-62.search.msn.com
etc...
I need a find and replace algorithm that apeends to the beginning and end of each line and replaces the delimeter, which in this case is just a space.
eg. the ouput after running the regex should be
'text1' 157.55.33.47 'text2' msnbot-157-55-33-47.search.msn.com 'text3'
'text1' 157.56.93.62 'text2' msnbot-157-56-93-62.search.msn.com 'text3'
Any guidance would be greatly appreciated!
Find what:
^([\S]+)\s([\S]+)$
Replace with:
'text1' $1 'text2' $2 'text3'
You can use a Macro for this instead of a regex. Record keystrokes on the first line. I'm on a Mac right now, so I can't be sure this is right, but it should be close to:
Home, [type 'text1'], CTRL+RightArrow [repeat 7 times], [type 'text2],
space, End, space, [type 'text3'], DownArrow
Once your Macro is recorded, re-run your Macro for the entire file. Again, I can't see the exact options, but it will be something like the following:
Go to Macros>Run a Macro Multiple Times..., select Current recorded macro, and Run until the end of file.

RegEx ActionScript 3 [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I am trying to figure out a regex to strip extra single quotes so that I would end up with only one single quote. To explain my question better, here is an example.
Let's say I have 3 different strings such as this ones.
(two single quotes)
Name<span fontSize=''16'' baselineShift=''superscript''>ABC</span>
(three single quotes)
Name<span fontSize='''16''' baselineShift='''superscript'''>ABC</span>
(four single quotes)
Name<span fontSize=''''16'''' baselineShift=''''superscript''''>ABC</span>
I am trying to sanitize the string to end up with this:
Name<span fontSize='16' baselineShift='superscript'>ABC</span>
I tried several online tools. This one is my favourite one: http://ryanswanson.com/regexp/#start. But I just can't get it right.
Could someone please help me out? Any tips and suggestions would be greatly appreciated.
Thank you in advance!
Did you try '+?
var str:String = "Name<span fontSize=''''16'''' baselineShift=''''superscript''''>ABC</span>";
trace( str.replace(/'+/g, "'") );
Have you looked at the docs for AS3's RegEx code? AS3 Replace
You could try something like this
var myPattern:RegExp = /'{2,100}/g;
var str:String = "fontSize=''''16''''";
trace(str.replace(myPattern, "'"));
The '{2,100} essentially looks for a match of ' that occurs between 2 - 100 times and replaces it with a single '.