Z
Z
Zaur Abdulgalimov2014-03-14 15:13:11
bash
Zaur Abdulgalimov, 2014-03-14 15:13:11

How to specify a flag (modifier) ​​of a regular expression in an if construct?

Design:

if 
  echo "$BASH_REMATCH"
fi

finds only the first match of the regular expression. How to find everything? Those. how to specify the g(global) modifier to search the entire string?
Or here is another example, there is a line like:
Id=1
Name=image
Url=image.png

You need to get the Name parameter. I write the following code:
name=` && echo "${BASH_REMATCH[1]}"`

How to specify the "m" modifier in this example so that the ^ and $ symbols mean the beginning and end of the line , and not the entire text .
PS Of course, on the shell there are many other ways to solve these problems, but I need to understand exactly how to specify modifiers in regular expressions .

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
RPG, 2014-03-14
@RPG

You cannot directly specify the flag, I can offer this option:

REG="Name=([^$IFS]*)"
 && echo "${BASH_REMATCH[1]}"

Why not... Perhaps because POSIX expressions are used.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question