Skip to main content

Questions tagged [regular-expressions]

For questions about the function RegularExpression which is used in string patterns in functions as StringCases, StringReplace, StringPosition,StringCount etc.

Filter by
Sorted by
Tagged with
3 votes
3 answers
107 views

Extract doi from a text string

I have a large list with bibliometric information including the doi like this example: ...
M.A.'s user avatar
  • 731
1 vote
1 answer
63 views

Difficulty using StringCases to obtain desired substring

I have extracted and stored some plaintext strings (derived from a pdfs) from the web that I wish to search for various values following certain text substrings that are different for different ...
Stuart Poss's user avatar
  • 1,933
3 votes
2 answers
140 views

Small trouble with variable referencing in StringReplace?

I am trying to do the following: Take a string abcdEFGpcf and replace whatever is between abcd and ...
Red Banana's user avatar
  • 5,411
2 votes
1 answer
88 views

Is there a function which checks the validity of a string to be used as argument to RegularExpression?

Is there a function which checks the validity of a string to be used as argument to RegularExpression? Composing regular expressions is not an easy task. In ...
Adalbert Hanßen's user avatar
1 vote
0 answers
41 views

A successful StringReplace with an error message [closed]

An example of StringReplace and RegularExpression : ...
imida k's user avatar
  • 4,315
6 votes
1 answer
90 views

Split a multi-line string at the beginning of each line using RegularExpression

I want to replicate the following code using RegularExpression instead of StartOfLine: ...
podge cassidy's user avatar
5 votes
2 answers
249 views

StringCases with string replace and with escape characters

I want to extract url address of all css files inside HTML source. First output is as expected. But why second and third outputs are different? Notice that only "href" was removed from ...
azerbajdzan's user avatar
  • 20.1k
4 votes
2 answers
2k views

How can I split strings with regex so that only numbers are selected

examples ...
mrz's user avatar
  • 11.7k
3 votes
3 answers
166 views

Regular Expressions in FileNameJoin[]

I'm attempting to create a list of folders that match a particular pattern. For example, consider I have the following files in a directory structure: ...
Karl's user avatar
  • 316
4 votes
1 answer
205 views

How to extract Table data using regular expressions?

For example, the Table have follows structure: ...
lumw's user avatar
  • 593
0 votes
0 answers
86 views

Why my code give empty plots? AND When I increase the number of mx , mt my code is running for a long time

In my code, two graphs are not displayed. what is the problem?Also when I increase the number of mx and mt, the code enters the running for a long time. The regularization problem is described as $D^{...
user68119's user avatar
5 votes
2 answers
240 views

using StringReplace and StringPattern to convert exp(...) to Exp[....]

I am looking at a file generated by another CAS. I'd like to convert it to Mathematica syntax. Only issue is how to convert exp(....) to ...
Nasser's user avatar
  • 147k
0 votes
0 answers
35 views

Correcting Import Errors presumably caused by unwanted whitespace characters

In scrapping data from a website that has a list of references searchable by the first letter of the author. I have attempted to Import this as a single html file for each letter. Oddly, this works ...
Stuart Poss's user avatar
  • 1,933
3 votes
1 answer
85 views

How to do context-sensitive matching with the pattern system?

In Perl RegEx, we have the look-ahead/behind feature; it there a similar functionality available for Wolfram Language patterns? Re @mikado: Suppose we have a random expression in ...
Gravifer's user avatar
  • 872
1 vote
3 answers
69 views

How can I let StringPosition consolidate overlapping matches?

Consider an example like this one: StringPosition[" -aa.ef -ag.hi -aa.ei", RegularExpression["\\s+-(?=a+\\.)"]] which returns ...
Adalbert Hanßen's user avatar
3 votes
1 answer
296 views

How can I let RegularExpression remove all colors from its regex-input string before processing it?

Regular expressions are a very powerful tool, but such expressions are often very cryptic. If you want to understand them, it is best to have colored pencils at hand to color-code the parts that ...
Adalbert Hanßen's user avatar
4 votes
2 answers
5k views

What is a regex which matches exactly m to n characters but not more?

StringCases["abcdefg hi jkl mn opq rstuv w xy z ", RegularExpression["[a-z]{1,3}"]] returns ...
Adalbert Hanßen's user avatar
5 votes
2 answers
1k views

using regex to find a specific word

I want to find the word "circle" and "circles" in the list. list = {"circle" , "picircle" , "circles" } The regex ...
Aryan's user avatar
  • 349
9 votes
2 answers
1k views

Using Regular Expression

sample2 = "this is a test to find whether pi works for other words or not as well pi pi pi potatpi pineapple pi-neapple"; so ...
Aryan's user avatar
  • 349
4 votes
3 answers
154 views

Extract the name of a point with Regular Expression

I have a list of points as strings in the form {"A1=(345.2345,3423.1)","B=(2123,97.123)","KX=(2144.546,-4455)"} and I would like to extract the names using ...
Emmet 's user avatar
  • 356
5 votes
4 answers
559 views

Regular Expression with +/-

Is it possible to extract the sign using Regular Expression ? For example {"Today+100","Yesterday-200"}. And I need {"Today",100,"Yesterday",-200}. Currently, I use Regular Expression to get the ...
Akcmok's user avatar
  • 197
2 votes
2 answers
439 views

Using regular expressions to replace constants by functions

I have an expression of the form: expression = A1*x + A2*x^2 + ... and I would like to replace A1 by ...
user26866's user avatar
  • 163
2 votes
0 answers
140 views

General rule to rewrite function and argument into a new symbol

I am trying to convert Mathematica output into equations I can plug into C. I am presently trying to convert expressions of the form Derivative[1,0,0][f][t,x,y] ...
physics_researcher's user avatar
5 votes
2 answers
549 views

Implementing a Better Grep Command

In Working With String Patterns, Wolfram documentation yields a simple Grep function: ...
George's user avatar
  • 3,145
5 votes
3 answers
138 views

StartOfLine and EndOfLine not working well inside Shortest

Suppose that I want to replace lines containing "b" with a new "XXX" line. This works as expected with regular expressions: ...
Gustavo Delfino's user avatar
0 votes
1 answer
82 views

RepeatedReplace (//.) not working as expected within an expression in a graph

I'm plotting a tangent line to a region and, depending when I evaluate it, I get inconsistent results. I want to use the first way of doing things for flexibility but it doesn't seem to be working. ...
GerardF123's user avatar
2 votes
1 answer
178 views

How to refer to a character group without ambiguity in a regex?

Suppose I want to realize a replacement in a string: "aabbcc11dd ee" -> "aabbcc112dd ee", I can use a regex like this within ...
Αλέξανδρος Ζεγγ's user avatar
0 votes
1 answer
83 views

Evaluate until a regexp match is satisfied

How do you express the computation that, given a list of regular expressions, perform a replacement until one of them is satisfied. For instance: ...
Dean's user avatar
  • 63
7 votes
0 answers
120 views

Mathematica lexer: Numbers

This is a follow-up question to Mathematica lexer: Symbols and UTF-8 and this it is about numbers. In Mathematica, we have a large number of ways to input numbers. They can contain a notation for the ...
halirutan's user avatar
  • 113k
3 votes
1 answer
50 views

StringExpression inconsistency using Alternative

Preface: This will turn out to be a bad case of brain-malfunction. In any case, the precedence is surprising and I haven't checked the FullForm... the one thing we ...
halirutan's user avatar
  • 113k
11 votes
0 answers
205 views

Mathematica lexer: Symbols and UTF-8

Did you ever wonder why here on our site, there is a highlighting difference between f[argX_] := 1; and f[argα_] := 1; ...
halirutan's user avatar
  • 113k
9 votes
4 answers
2k views

How to convert a string having nested parentheses into an expression?

I am a Wolfram Language/Mathematica beginner and trying to convert STEP ISO10303 commands into geometrical regions. A simple command looks like ...
markilux's user avatar
  • 121
2 votes
2 answers
60 views

Getting line number based on regexp match or Importing data with variable header size

I have a text file with structure: ...
atapaka's user avatar
  • 4,006
5 votes
1 answer
478 views

Use cases to select subelements by Regex

I have a nested list and would like to select a sub set of lists using a RegEx and I would prefer to use patterns (mainly for readability). ...
Dennis Guse's user avatar
1 vote
2 answers
1k views

regex to find \n within quotes and not ones outside quotes

For example, string = "ads, 32, dv, \"sdf\" sd, 213, as, \"asd asd\" asd, 123 sd, \"asd\""; There are 3 line returns \n....
user13892's user avatar
  • 9,523
8 votes
1 answer
334 views

RegularExpression no longer working in Mathematica 10.4

There is a regular expression that I used often in Mathematica 8, made to locate matching curly braces: RegularExpression["(?P<0>{([^{}]|(?P>0))*})"] ...
Denis Cousineau's user avatar
8 votes
4 answers
618 views

RegEx replacement with backreference followed by a numeric literal

I have a text file which contains lines of the type: SYMATTR InstName U1 where "1" can be actually a number with 1 to 3 digits. I want to replace the above line with the following 2 lines: ...
Massimo Ortolano's user avatar
5 votes
2 answers
82 views

How to match numbers to alphabets with period and space,

ToCharacterCode["ABCD...XYZ"] - 64 I know that I can make a list of alphabets in this way. However, I need to assign " " and <...
Lee's user avatar
  • 321
6 votes
1 answer
172 views

Regex named groups -- how to refer back to them in the replacement string [duplicate]

We can use group numbers to reuse the same piece of code while inside regex string, like that: StringCases["x = y", RegularExpression["([\\w\\s]+)=(?1)"]] <...
Stitch's user avatar
  • 4,225
27 votes
1 answer
633 views

Why Except is so slow as compared to equivalent RegularExpression?

When comparing performance of the solutions suggested for this question I discovered that StringExpression involving Except is ...
Alexey Popkov's user avatar
6 votes
3 answers
668 views

Extract substring up to specific character non-inclusive

I was wondering if there is a way with StringCases to start at the beginning of a string and stop one character before a known character. ...
newtonjeep's user avatar
7 votes
3 answers
429 views

dictionary lookups of words composed of specified characters

I am trying to find all English words composed of specified characters. For example, all words composed of one or more of the following: ...
Michael Stern's user avatar
0 votes
1 answer
121 views

How to fix this issue with storing an expression as a String and returning it with ToExpression

( Update: Note the "." in the expression, this is most likely what causes the problem. )** Starting point of my question is the following code, which works as expected: ...
nilo de roock's user avatar
6 votes
1 answer
413 views

How to iterate over a list using a Regex

I need to replace all "Nx" with "N0x" in the following, but I can't seem to figure it out. ...
Janus Syrak's user avatar
11 votes
4 answers
2k views

How to get the longest bracket pairs from a string

In C#, we can use regular expression to do some balancing groups, like we can get {<aa <bbb> <bbb> aa>, <<dfa>a>} from ...
yode's user avatar
  • 26.9k
14 votes
1 answer
155 views

What types of string patterns can't be translated into regexes and hence do not work?

In a comment to related question Oleksandr R. made an interesting remark: ... because string patterns are converted into regular expressions and matched using PCRE, they are subject to some ...
Alexey Popkov's user avatar
9 votes
1 answer
182 views

Can we use named references on the RHS in StringReplace?

In this great answer WReach demonstrated some advantages of usage of Named Patterns inside of RegularExpression and described the supported syntax: PCRE in ...
Alexey Popkov's user avatar
0 votes
2 answers
113 views

Can I use StringExpression or RegularExpression to manipulate Symbolic XML?

Please, consider an XML document containing these fields: ...
mitochondrial's user avatar
22 votes
2 answers
643 views

Shell Emulation inside Mathematica

I am trying to create a Mathematica/shell syntax hybrid. I would like the following command wget -qO- "http://google.com/space line break\"" $var to be ...
William's user avatar
  • 7,635
4 votes
1 answer
426 views

Extracting integers from a string

Imagine I have a block of text that contains words and numbers. I want to extract all the integers from the text. Thus, if the input is ...
Michael Stern's user avatar